From fad7818bf0f2fc60b7713ee39773749c09405773 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 12 Jan 2018 11:46:43 +0300 Subject: [PATCH] Fix "if to safe access" false positive for check on incorrect type So #KT-21881 Fixed --- .../IfThenToSafeAccessInspection.kt | 3 ++- .../branchedTransformations/IfThenUtils.kt | 6 ++++-- .../implicitReceiverInApply.kt | 17 +++++++++++++++++ .../LocalInspectionTestGenerated.java | 6 ++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt index ce5a9b761f0..326669538fc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToSafeAccessInspection.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression +import org.jetbrains.kotlin.resolve.calls.callUtil.getType class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection(KtIfExpression::class.java) { @@ -84,6 +85,6 @@ class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection false negatedClause != null && !negatedClause.isNullExpression() -> false else -> baseClause.evaluatesTo(receiverExpression) || baseClause.hasFirstReceiverOf(receiverExpression) || - receiverExpression is KtThisExpression && hasImplicitReceiver() + receiverExpression is KtThisExpression && getImplicitReceiver()?.let { it.type == receiverExpression.getType(context) } == true } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index d2fcf611534..28c02623769 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -40,6 +40,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverValue import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf import org.jetbrains.kotlin.utils.addToStdlib.constant @@ -190,8 +191,9 @@ data class IfThenToSelectData( } } - internal fun hasImplicitReceiver(): Boolean = - baseClause.getResolvedCall(context)?.getImplicitReceiverValue() != null + internal fun getImplicitReceiver(): ImplicitReceiver? = baseClause.getResolvedCall(context)?.getImplicitReceiverValue() + + internal fun hasImplicitReceiver(): Boolean = getImplicitReceiver() != null } internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData? { diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt new file mode 100644 index 00000000000..dcf80741446 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt @@ -0,0 +1,17 @@ +// PROBLEM: none +// WITH_RUNTIME +class Foo + +class Test { + fun foo(): Any = "" + + fun bar() {} + + fun test(a: Any) { + foo().apply { + if (this is Foo) { + bar() + } + } + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index ca5a8266652..7c3be5c1574 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -140,6 +140,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + @TestMetadata("implicitReceiverInApply.kt") + public void testImplicitReceiverInApply() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt"); + doTest(fileName); + } + @TestMetadata("isCheckSimple.kt") public void testIsCheckSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/isCheckSimple.kt");