check return type of contains() and compareTo()

This commit is contained in:
Dmitry Jemerov
2015-09-24 16:54:32 +02:00
parent 3dbb74bf0c
commit 41d1cf6083
3 changed files with 26 additions and 3 deletions
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.* import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetModifierListOwner import org.jetbrains.kotlin.psi.JetModifierListOwner
@@ -51,9 +53,9 @@ public class OperatorModifierInspection : AbstractKotlinInspection(), CleanupLoc
return true return true
} }
if (arity == 1 && (name in OperatorConventions.BINARY_OPERATION_NAMES.values() || if (arity == 1 && (name in OperatorConventions.BINARY_OPERATION_NAMES.values() ||
name in OperatorConventions.ASSIGNMENT_OPERATIONS.values () || name in OperatorConventions.ASSIGNMENT_OPERATIONS.values () ||
name == OperatorConventions.CONTAINS || (name == OperatorConventions.CONTAINS && isBooleanReturnType()) ||
name == OperatorConventions.COMPARE_TO)) { (name == OperatorConventions.COMPARE_TO && isIntReturnType()))) {
return true return true
} }
if (name == OperatorConventions.INVOKE) { if (name == OperatorConventions.INVOKE) {
@@ -67,6 +69,14 @@ public class OperatorModifierInspection : AbstractKotlinInspection(), CleanupLoc
} }
return false return false
} }
private fun JetNamedFunction.isIntReturnType(): Boolean {
return KotlinBuiltIns.isInt(analyze().getType(this) ?: return false)
}
private fun JetNamedFunction.isBooleanReturnType(): Boolean {
return KotlinBuiltIns.isBoolean(analyze().getType(this) ?: return false)
}
} }
class AddModifierLocalQuickFix(val modifier: JetModifierKeywordToken) : LocalQuickFix { class AddModifierLocalQuickFix(val modifier: JetModifierKeywordToken) : LocalQuickFix {
@@ -0,0 +1,7 @@
// "Add 'operator' modifier" "false"
// ACTION: Convert member to extension
// ACTION: Convert to block body
// ACTION: Remove explicit type specification
class A {
fun <caret>contains(other: A): Int = -1
}
@@ -4195,6 +4195,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/operatorModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/operatorModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
} }
@TestMetadata("containsInt.kt")
public void testContainsInt() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/containsInt.kt");
doTest(fileName);
}
@TestMetadata("simple.kt") @TestMetadata("simple.kt")
public void testSimple() throws Exception { public void testSimple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/simple.kt"); String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/simple.kt");