From be30761ec46f25438dd3bf4ff1a56677c0a07f58 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 16 Aug 2018 11:21:34 +0900 Subject: [PATCH] "Convert to 'also'" intention: Fix for call expression has this receiver #KT-26009 Fixed --- .../idea/inspections/ScopeFunctionConversionInspection.kt | 6 ++++-- .../scopeFunctions/applyToAlso/methodWithThis.kt | 6 ++++++ .../scopeFunctions/applyToAlso/methodWithThis.kt.after | 6 ++++++ .../idea/inspections/LocalInspectionTestGenerated.java | 5 +++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt create mode 100644 idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt index 8d94803d53b..43f51a68e7e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt @@ -209,8 +209,10 @@ class ConvertScopeFunctionToParameter(counterpartName: String) : ConvertScopeFun if (dispatchReceiverTarget == lambdaDescriptor || extensionReceiverTarget == lambdaDescriptor) { val parent = expression.parent if (parent is KtCallExpression && expression == parent.calleeExpression) { - replacements.add(parent) { element -> - factory.createExpressionByPattern("$0.$1", parameterName, element) + if ((parent.parent as? KtQualifiedExpression)?.receiverExpression !is KtThisExpression) { + replacements.add(parent) { element -> + factory.createExpressionByPattern("$0.$1", parameterName, element) + } } } else if (parent is KtQualifiedExpression && parent.receiverExpression is KtThisExpression) { // do nothing diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt new file mode 100644 index 00000000000..74f97cf7a0a --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +val x = hashSetOf().apply { + this.add("x") +} diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt.after b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt.after new file mode 100644 index 00000000000..15b6c3979bd --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +val x = hashSetOf().also { + it.add("x") +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index c134ebd2267..442b25671c0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5410,6 +5410,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/method.kt"); } + @TestMetadata("methodWithThis.kt") + public void testMethodWithThis() throws Exception { + runTest("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/methodWithThis.kt"); + } + @TestMetadata("outerLambda.kt") public void testOuterLambda() throws Exception { runTest("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/outerLambda.kt");