"Unused equals expression" inspection: highlight whole expression with yellow background

^KT-41615 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-09-04 09:45:15 +02:00
committed by Vladimir Dolzhenko
parent b81ec2c8f0
commit 343010a833
9 changed files with 103 additions and 23 deletions
+1 -1
View File
@@ -1454,7 +1454,7 @@
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnusedEqualsInspection"
groupPath="Kotlin"
groupName="Redundant constructs"
groupName="Probable bugs"
enabledByDefault="true"
level="WARNING"
language="kotlin"
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.KotlinBundle
@@ -21,15 +20,10 @@ class UnusedEqualsInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
private fun reportIfNotUsedAsExpression(expression: KtExpression, target: KtExpression) {
private fun reportIfNotUsedAsExpression(expression: KtExpression) {
val context = expression.analyze()
if (!expression.isUsedAsExpression(context)) {
holder.registerProblem(
target,
KotlinBundle.message("unused.equals.expression"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL
)
holder.registerProblem(expression, KotlinBundle.message("unused.equals.expression"))
}
}
@@ -38,7 +32,7 @@ class UnusedEqualsInspection : AbstractKotlinInspection() {
if (expression.operationToken == KtTokens.EQEQ &&
(expression.parent is KtBlockExpression || expression.parent.parent is KtIfExpression)
) {
reportIfNotUsedAsExpression(expression, expression.operationReference)
reportIfNotUsedAsExpression(expression)
}
}
@@ -49,7 +43,7 @@ class UnusedEqualsInspection : AbstractKotlinInspection() {
if (calleeExpression.getReferencedNameAsName() != OperatorNameConventions.EQUALS) return
if (!expression.isAnyEquals()) return
reportIfNotUsedAsExpression(expression.getQualifiedExpressionForSelectorOrThis(), calleeExpression)
reportIfNotUsedAsExpression(expression.getQualifiedExpressionForSelectorOrThis())
}
}
}
@@ -4,8 +4,11 @@
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == b</highlighted_element>
<offset>4</offset>
<length>6</length>
</problem>
<problem>
@@ -13,8 +16,11 @@
<line>5</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == 1</highlighted_element>
<offset>4</offset>
<length>6</length>
</problem>
<problem>
@@ -22,8 +28,11 @@
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == b</highlighted_element>
<offset>8</offset>
<length>6</length>
</problem>
<problem>
@@ -31,8 +40,11 @@
<line>13</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == b</highlighted_element>
<offset>8</offset>
<length>6</length>
</problem>
<problem>
@@ -40,8 +52,11 @@
<line>14</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == b</highlighted_element>
<offset>8</offset>
<length>6</length>
</problem>
<problem>
@@ -49,8 +64,11 @@
<line>18</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == b</highlighted_element>
<offset>4</offset>
<length>6</length>
</problem>
<problem>
@@ -58,8 +76,11 @@
<line>38</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="simple.kt"/>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == 1</highlighted_element>
<offset>12</offset>
<length>6</length>
</problem>
<problem>
@@ -67,8 +88,11 @@
<line>61</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == -1</highlighted_element>
<offset>28</offset>
<length>7</length>
</problem>
<problem>
@@ -76,8 +100,11 @@
<line>61</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>a == -1</highlighted_element>
<offset>28</offset>
<length>7</length>
</problem>
<problem>
@@ -85,8 +112,11 @@
<line>71</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>it == null</highlighted_element>
<offset>34</offset>
<length>10</length>
</problem>
<problem>
@@ -94,8 +124,11 @@
<line>75</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>it.equals(null)</highlighted_element>
<offset>34</offset>
<length>15</length>
</problem>
<problem>
@@ -103,7 +136,10 @@
<line>81</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused equals expression</problem_class>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused equals expression</problem_class>
<description>Unused equals expression</description>
<highlighted_element>equals(null)</highlighted_element>
<offset>48</offset>
<length>12</length>
</problem>
</problems>
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.UnusedEqualsInspection
+4
View File
@@ -0,0 +1,4 @@
// FIX: none
fun foo(a: Int, b: Int) {
<caret>a.equals(b)
}
@@ -0,0 +1,4 @@
// FIX: none
fun foo(a: Int, b: Int) {
a.equals(b)<caret>
}
+4
View File
@@ -0,0 +1,4 @@
// FIX: none
fun foo(a: Int, b: Int) {
<caret>a == b
}
@@ -0,0 +1,4 @@
// FIX: none
fun foo(a: Int, b: Int) {
a == b<caret>
}
@@ -13984,6 +13984,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/unusedEquals")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class UnusedEquals extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInUnusedEquals() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/unusedEquals"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("equals.kt")
public void testEquals() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedEquals/equals.kt");
}
@TestMetadata("equals2.kt")
public void testEquals2() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedEquals/equals2.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedEquals/simple.kt");
}
@TestMetadata("simple2.kt")
public void testSimple2() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedEquals/simple2.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)