From 3f2c73b4a93d974b0bc912633c1ac9e897726c19 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 10 Sep 2018 14:15:08 +0900 Subject: [PATCH] if-then to safe access: fix for qualified 'this' condition #KT-26662 Fixed --- .../intentions/branchedTransformations/IfThenUtils.kt | 2 +- .../branched/ifThenToSafeAccess/implicitReceiver2.kt | 9 +++++++++ .../ifThenToSafeAccess/implicitReceiver2.kt.after | 7 +++++++ .../branched/ifThenToSafeAccess/implicitReceiver3.kt | 10 ++++++++++ .../ifThenToSafeAccess/implicitReceiver3.kt.after | 8 ++++++++ .../idea/inspections/LocalInspectionTestGenerated.java | 10 ++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt.after create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt.after 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 135e26ace7d..94a98824f6d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -228,7 +228,7 @@ data class IfThenToSelectData( else -> error("Illegal state") } } - hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern("this?.$0", baseClause).insertSafeCalls(factory) + hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern("$0?.$1", receiverExpression, baseClause).insertSafeCalls(factory) baseClause is KtCallExpression -> baseClause.replaceCallWithLet(receiverExpression, factory) else -> baseClause.insertSafeCalls(factory) } diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt new file mode 100644 index 00000000000..ffdff2eed26 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt @@ -0,0 +1,9 @@ +class Foo { + fun foo() {} +} + +fun Foo?.test() { + if (this@test != null) { + foo() + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt.after new file mode 100644 index 00000000000..885a3cbd078 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt.after @@ -0,0 +1,7 @@ +class Foo { + fun foo() {} +} + +fun Foo?.test() { + this@test?.foo() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt new file mode 100644 index 00000000000..593800507fe --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt @@ -0,0 +1,10 @@ +class Foo + +class Bar { + fun Foo?.test() { + if (this@Bar != null) { + bar() + } + } + fun bar() {} +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt.after new file mode 100644 index 00000000000..848c9a9a10d --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt.after @@ -0,0 +1,8 @@ +class Foo + +class Bar { + fun Foo?.test() { + this@Bar?.bar() + } + fun bar() {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 782e2a5aafd..664deee1f1c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -166,6 +166,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver.kt"); } + @TestMetadata("implicitReceiver2.kt") + public void testImplicitReceiver2() throws Exception { + runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver2.kt"); + } + + @TestMetadata("implicitReceiver3.kt") + public void testImplicitReceiver3() throws Exception { + runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiver3.kt"); + } + @TestMetadata("implicitReceiverInApply.kt") public void testImplicitReceiverInApply() throws Exception { runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/implicitReceiverInApply.kt");