mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-04 15:51:54 +00:00
Add inspection that reports unused equals expression #KT-6217 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
73845bfc27
commit
778289fefe
5
idea/resources/inspectionDescriptions/UnusedEquals.html
Normal file
5
idea/resources/inspectionDescriptions/UnusedEquals.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports unused equals expressions.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2001,6 +2001,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnusedEqualsInspection"
|
||||
displayName="Unused equals expression"
|
||||
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"/>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtIfExpression
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
|
||||
|
||||
class UnusedEqualsInspection : AbstractKotlinInspection() {
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitBinaryExpression(expression: KtBinaryExpression) {
|
||||
super.visitBinaryExpression(expression)
|
||||
if (expression.operationToken == KtTokens.EQEQ &&
|
||||
(expression.parent is KtBlockExpression || expression.parent.parent is KtIfExpression)) {
|
||||
val context = expression.analyze()
|
||||
if (!expression.isUsedAsExpression(context)) {
|
||||
holder.registerProblem(expression.operationReference,
|
||||
"Unused equals expression",
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
82
idea/testData/inspections/unusedEquals/inspectionData/expected.xml
vendored
Normal file
82
idea/testData/inspections/unusedEquals/inspectionData/expected.xml
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<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>
|
||||
<description>Unused equals expression</description>
|
||||
</problem>
|
||||
</problems>
|
||||
1
idea/testData/inspections/unusedEquals/inspectionData/inspections.test
vendored
Normal file
1
idea/testData/inspections/unusedEquals/inspectionData/inspections.test
vendored
Normal file
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedEqualsInspection
|
||||
64
idea/testData/inspections/unusedEquals/test.kt
vendored
Normal file
64
idea/testData/inspections/unusedEquals/test.kt
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(a: Int, b: Int) {
|
||||
a == b // not used
|
||||
a == 1 // not used
|
||||
|
||||
foo2 {
|
||||
a == b // not used
|
||||
a == b // used
|
||||
}
|
||||
|
||||
foo3 {
|
||||
a == b // not used
|
||||
a == b // not used
|
||||
}
|
||||
|
||||
val e = a == b // used
|
||||
a == b // not used
|
||||
}
|
||||
|
||||
fun foo2(c: () -> Boolean) {
|
||||
|
||||
}
|
||||
|
||||
fun foo3(d: () -> Unit) {
|
||||
|
||||
}
|
||||
|
||||
fun foo4(a: Int): Boolean {
|
||||
return a == 1 // used
|
||||
}
|
||||
|
||||
fun foo5(a: Int) = a == 1 // used
|
||||
|
||||
fun foo6(a: Int) {
|
||||
foo2 {
|
||||
fun foo7() {
|
||||
a == 1 // not used
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun foo7(a: Int) {
|
||||
run {
|
||||
a == b // used as return value of run
|
||||
}
|
||||
|
||||
if (a == b) return // used
|
||||
}
|
||||
|
||||
fun foo8(a: Int) {
|
||||
val eq = if (a > 1) a == 10 else a == -1 // used
|
||||
|
||||
val eq2 = if (a > 1) {
|
||||
a == 10 // used
|
||||
}
|
||||
else {
|
||||
a == -1 // used
|
||||
}
|
||||
|
||||
if (a > 1) a == 10 else a == -1 // both unused
|
||||
|
||||
if (a == 10) {} else if (a == -1) {} // both used
|
||||
}
|
||||
@@ -287,6 +287,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unusedEquals/inspectionData/inspections.test")
|
||||
public void testUnusedEquals_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedEquals/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unusedImport/inspectionData/inspections.test")
|
||||
public void testUnusedImport_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedImport/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user