Replace call with comparison inspection introduced #KT-11425 Fixed
(cherry picked from commit 042fc0d)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
fc13b25ca4
commit
475dc6541b
@@ -0,0 +1,4 @@
|
|||||||
|
<html><body>
|
||||||
|
This inspection reports equals() and compareTo() calls that can be replaced with binary comparison.
|
||||||
|
Example: <code>2.compareTo(1) > 0</code> can be replaced by <code>2 > 1</code>.
|
||||||
|
</body></html>
|
||||||
@@ -1636,6 +1636,14 @@
|
|||||||
language="kotlin"
|
language="kotlin"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceCallWithComparisonInspection"
|
||||||
|
displayName="Can be replaced with comparison"
|
||||||
|
groupName="Kotlin"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WARNING"
|
||||||
|
language="kotlin"
|
||||||
|
/>
|
||||||
|
|
||||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
+12
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.intention.HighPriorityAction
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.tree.IElementType
|
import com.intellij.psi.tree.IElementType
|
||||||
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.intentions.*
|
import org.jetbrains.kotlin.idea.intentions.*
|
||||||
import org.jetbrains.kotlin.lexer.KtSingleValueToken
|
import org.jetbrains.kotlin.lexer.KtSingleValueToken
|
||||||
@@ -33,6 +34,17 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|||||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||||
|
|
||||||
|
class ReplaceCallWithComparisonInspection(
|
||||||
|
val intention: ReplaceCallWithBinaryOperatorIntention = ReplaceCallWithBinaryOperatorIntention()
|
||||||
|
) : IntentionBasedInspection<KtDotQualifiedExpression>(
|
||||||
|
intention,
|
||||||
|
{ qualifiedExpression ->
|
||||||
|
val calleeExpression = qualifiedExpression.callExpression?.calleeExpression as? KtSimpleNameExpression
|
||||||
|
val identifier = calleeExpression?.getReferencedNameAsName()
|
||||||
|
identifier == OperatorNameConventions.EQUALS || identifier == OperatorNameConventions.COMPARE_TO
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
class ReplaceCallWithBinaryOperatorIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(
|
class ReplaceCallWithBinaryOperatorIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(
|
||||||
KtDotQualifiedExpression::class.java,
|
KtDotQualifiedExpression::class.java,
|
||||||
"Replace call with binary operator"
|
"Replace call with binary operator"
|
||||||
|
|||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>2</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with comparison</problem_class>
|
||||||
|
<description>Replace with '==' operator</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>3</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with comparison</problem_class>
|
||||||
|
<description>Replace with '==' operator</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>6</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with comparison</problem_class>
|
||||||
|
<description>Replace with '>' operator</description>
|
||||||
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>7</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with comparison</problem_class>
|
||||||
|
<description>Replace with '<=' operator</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceCallWithComparisonInspection
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun foo() {
|
||||||
|
1.equals(1) // YES
|
||||||
|
!1.equals(2) // YES
|
||||||
|
1.compareTo(1) // NO
|
||||||
|
1.compareTo(1) == 0 // NO
|
||||||
|
2.compareTo(1) > 0 // YES
|
||||||
|
0 >= 1.compareTo(2) // YES
|
||||||
|
2.plus(2) // NO
|
||||||
|
2.times(2) // NO
|
||||||
|
}
|
||||||
@@ -226,6 +226,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("replaceCallWithComparison/inspectionData/inspections.test")
|
||||||
|
public void testReplaceCallWithComparison_inspectionData_Inspections_test() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/replaceCallWithComparison/inspectionData/inspections.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("spelling/inspectionData/inspections.test")
|
@TestMetadata("spelling/inspectionData/inspections.test")
|
||||||
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user