"Suspicious ==/===": do not report when comparison with null is included

So #KT-24001 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-04-26 13:03:15 +03:00
committed by Mikhail Glukhikh
parent d15e43f38a
commit 4d1b8405cb
3 changed files with 27 additions and 4 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isNullExpression
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -29,12 +30,16 @@ class SuspiciousEqualsCombination : AbstractKotlinInspection() {
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)
if (!left.isNullExpression() && !right.isNullExpression()) {
(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)
if (!left.isNullExpression() && !right.isNullExpression()) {
(left as? KtNameReferenceExpression)?.let(pair.eqEqEqOperands::add)
(right as? KtNameReferenceExpression)?.let(pair.eqEqEqOperands::add)
}
}
KtTokens.ANDAND, KtTokens.OROR -> {
right?.parseExpression(pair)
@@ -34,4 +34,13 @@
<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>18</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>
@@ -7,6 +7,15 @@ object Main {
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 type2: Main? = null
if (type2 == null || type === type2) return
val type3: Main? = null
if (type3 === null || type == type3) return
val type4: Main? = null
if (type4 == null || type === type2 || type1 == type) return
}
val CONST1 = Main;