IfThenToSafeAccessInspection: do not report if condition is SENSELESS_COMPARISON/USELESS_IS_CHECK (#3007)

#KT-36051 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-08-19 18:22:10 +09:00
committed by GitHub
parent 122bba9102
commit d965ad0a98
7 changed files with 34 additions and 17 deletions
@@ -9,6 +9,7 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.imports.importableFqName
@@ -100,6 +101,8 @@ private fun IfThenToSelectData.clausesReplaceableBySafeCall(): Boolean = when {
baseClause == null -> false
negatedClause == null && baseClause.isUsedAsExpression(context) -> false
negatedClause != null && !negatedClause.isNullExpression() -> false
context.diagnostics.forElement(condition)
.any { it.factory == Errors.SENSELESS_COMPARISON || it.factory == Errors.USELESS_IS_CHECK } -> false
baseClause.evaluatesTo(receiverExpression) -> true
baseClause.hasFirstReceiverOf(receiverExpression) -> withoutResultInCallChain(baseClause, context)
baseClause.anyArgumentEvaluatesTo(receiverExpression) -> true
@@ -1,3 +1,4 @@
// PROBLEM: none
class Foo
class Bar {
@@ -1,8 +0,0 @@
class Foo
class Bar {
fun Foo?.test() {
this@Bar?.bar()
}
fun bar() {}
}
@@ -136,15 +136,6 @@
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Foldable if-then</description>
</problem>
<problem>
<file>implicitReceiver3.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/implicitReceiver3.kt" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Foldable if-then</description>
</problem>
<problem>
<file>resultCall.kt</file>
<line>6</line>
@@ -0,0 +1,10 @@
// PROBLEM: none
fun test(foo: Foo) {
<caret>if (foo != null) {
foo.bar()
}
}
class Foo {
fun bar() {}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun test(foo: Foo) {
<caret>if (foo is Foo) {
foo.bar()
}
}
class Foo {
fun bar() {}
}
@@ -757,6 +757,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/rhsNotEqualsNull.kt");
}
@TestMetadata("senselessComparison.kt")
public void testSenselessComparison() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/senselessComparison.kt");
}
@TestMetadata("thenAndElseBothNull.kt")
public void testThenAndElseBothNull() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/thenAndElseBothNull.kt");
@@ -787,6 +792,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/unacceptableNoThenBlock.kt");
}
@TestMetadata("uselessIsCheck.kt")
public void testUselessIsCheck() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/uselessIsCheck.kt");
}
@TestMetadata("willNotInlineClassProperty.kt")
public void testWillNotInlineClassProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/willNotInlineClassProperty.kt");