Replace call with comparison inspection introduced #KT-11425 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-07-28 11:56:59 +03:00
parent 7219904a87
commit 042fc0d3b8
7 changed files with 75 additions and 0 deletions

View File

@@ -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>

View File

@@ -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"/>

View File

@@ -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"

View 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 '&gt;' 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 '&lt;=' operator</description>
</problem>
</problems>

View File

@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceCallWithComparisonInspection

View 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
}

View File

@@ -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");