check return type of contains() and compareTo()
This commit is contained in:
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.idea.inspections
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
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.JetTokens
|
||||
import org.jetbrains.kotlin.psi.JetModifierListOwner
|
||||
@@ -51,9 +53,9 @@ public class OperatorModifierInspection : AbstractKotlinInspection(), CleanupLoc
|
||||
return true
|
||||
}
|
||||
if (arity == 1 && (name in OperatorConventions.BINARY_OPERATION_NAMES.values() ||
|
||||
name in OperatorConventions.ASSIGNMENT_OPERATIONS.values () ||
|
||||
name == OperatorConventions.CONTAINS ||
|
||||
name == OperatorConventions.COMPARE_TO)) {
|
||||
name in OperatorConventions.ASSIGNMENT_OPERATIONS.values () ||
|
||||
(name == OperatorConventions.CONTAINS && isBooleanReturnType()) ||
|
||||
(name == OperatorConventions.COMPARE_TO && isIntReturnType()))) {
|
||||
return true
|
||||
}
|
||||
if (name == OperatorConventions.INVOKE) {
|
||||
@@ -67,6 +69,14 @@ public class OperatorModifierInspection : AbstractKotlinInspection(), CleanupLoc
|
||||
}
|
||||
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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@TestMetadata("containsInt.kt")
|
||||
public void testContainsInt() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/containsInt.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/simple.kt");
|
||||
|
||||
Reference in New Issue
Block a user