KT-17888 Inspection to warn about suspicious combination of == and ===

This commit is contained in:
Dimach
2017-08-14 21:19:33 +03:00
committed by Dmitry Jemerov
parent 83a1d6e5ca
commit b988fb701d
7 changed files with 142 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports when == and === used on same variable in one expression
</body>
</html>
+9
View File
@@ -2546,6 +2546,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.SuspiciousEqualsCombination"
displayName="Suspicious combination of == and ==="
groupPath="Kotlin"
groupName="Probable bugs"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2017 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.CleanupLocalInspectionTool
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
class SuspiciousEqualsCombination : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
object : KtVisitorVoid() {
override fun visitBinaryExpression(expression: KtBinaryExpression) {
if (expression.parent is KtBinaryExpression) return
val operands = expression.parseBinary()
val eqeq = operands.eqEqOperands.map { it.text }
val eqeqeq = operands.eqEqEqOperands.map { it.text }
if (eqeq.intersect(eqeqeq).isNotEmpty()) {
holder.registerProblem(expression, "Suspicious combination of == and ===",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING)
}
}
}
private fun KtBinaryExpression.parseBinary(pair: ComparisonOperands = ComparisonOperands()): ComparisonOperands {
when (operationToken) {
KtTokens.EQEQ, KtTokens.EXCLEQ -> {
(left as? KtNameReferenceExpression)?.let(pair.eqEqOperands::add)
(right as? KtNameReferenceExpression)?.let(pair.eqEqOperands::add)
}
KtTokens.EQEQEQ, KtTokens.EXCLEQEQEQ -> {
(left as? KtNameReferenceExpression)?.let(pair.eqEqEqOperands::add)
(right as? KtNameReferenceExpression)?.let(pair.eqEqEqOperands::add)
}
KtTokens.ANDAND, KtTokens.OROR -> {
right?.parseExpression(pair)
left?.parseExpression(pair)
}
}
return pair
}
private fun KtExpression.parseExpression(pair: ComparisonOperands) {
when (this) {
is KtBinaryExpression -> parseBinary(pair)
is KtParenthesizedExpression -> expression?.parseExpression(pair)
is KtPrefixExpression -> if (operationToken == KtTokens.EXCL) baseExpression?.parseExpression(pair)
}
}
}
private data class ComparisonOperands(val eqEqOperands: MutableList<KtExpression> = mutableListOf(),
val eqEqEqOperands: MutableList<KtExpression> = mutableListOf())
@@ -0,0 +1,37 @@
<problems>
<problem>
<file>test.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Suspicious combination of == and ===</problem_class>
<description>Suspicious combination of == and ===</description>
</problem>
<problem>
<file>test.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Suspicious combination of == and ===</problem_class>
<description>Suspicious combination of == and ===</description>
</problem>
<problem>
<file>test.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Suspicious combination of == and ===</problem_class>
<description>Suspicious combination of == and ===</description>
</problem>
<problem>
<file>test.kt</file>
<line>9</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Suspicious combination of == and ===</problem_class>
<description>Suspicious combination of == and ===</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.SuspiciousEqualsCombination
@@ -0,0 +1,15 @@
object Main {
fun test() {
val type1 = Main
val type = Main
if (type === CONST1 || type == CONST2 && type === CONST3) return
if (type === CONST1 || type == CONST2 && (type === CONST3)) return
if (type === CONST1 || type == CONST2 && !(type === CONST3)) return
if (type === CONST1 || type1 == CONST2 && type === CONST3) return
if (type === CONST1 || type1 == CONST1 && type === CONST3) return
}
val CONST1 = Main;
val CONST2 = Main;
val CONST3 = Main;
}
@@ -377,6 +377,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
doTest(fileName);
}
@TestMetadata("suspiciousEqualsCombination/inspectionData/inspections.test")
public void testSuspiciousEqualsCombination_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/suspiciousEqualsCombination/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("twoSetOfTypeparameters/inspectionData/inspections.test")
public void testTwoSetOfTypeparameters_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/twoSetOfTypeparameters/inspectionData/inspections.test");