mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-09 00:21:30 +00:00
Replace call with comparison inspection introduced #KT-11425 Fixed
This commit is contained in:
@@ -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>
|
||||
@@ -1610,6 +1610,14 @@
|
||||
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"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.intention.HighPriorityAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.intentions.*
|
||||
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.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>(
|
||||
KtDotQualifiedExpression::class.java,
|
||||
"Replace call with binary operator"
|
||||
|
||||
34
idea/testData/inspections/replaceCallWithComparison/inspectionData/expected.xml
vendored
Normal file
34
idea/testData/inspections/replaceCallWithComparison/inspectionData/expected.xml
vendored
Normal file
@@ -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
idea/testData/inspections/replaceCallWithComparison/inspectionData/inspections.test
vendored
Normal file
1
idea/testData/inspections/replaceCallWithComparison/inspectionData/inspections.test
vendored
Normal file
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceCallWithComparisonInspection
|
||||
10
idea/testData/inspections/replaceCallWithComparison/test.kt
vendored
Normal file
10
idea/testData/inspections/replaceCallWithComparison/test.kt
vendored
Normal file
@@ -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
|
||||
}
|
||||
@@ -220,6 +220,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
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")
|
||||
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user