diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt index 979331d7ba4..36438331d48 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt @@ -154,6 +154,9 @@ abstract class ConvertScopeFunctionFix(private val counterpartName: String) : Lo val functionLiteral = lambda.getLambdaExpression()?.functionLiteral ?: return val lambdaDescriptor = bindingContext[FUNCTION, functionLiteral] ?: return + functionLiteral.valueParameterList?.delete() + functionLiteral.arrow?.delete() + val replacements = ReplacementCollection() analyzeLambda(bindingContext, lambda, lambdaDescriptor, replacements) callee.replace(KtPsiFactory(project).createExpression(counterpartName) as KtNameReferenceExpression) @@ -205,6 +208,7 @@ class ConvertScopeFunctionToParameter(counterpartName: String) : ConvertScopeFun lambda.accept(object : KtTreeVisitorVoid() { override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) { super.visitSimpleNameExpression(expression) + if (expression is KtOperationReferenceExpression) return val resolvedCall = expression.getResolvedCall(bindingContext) ?: return val dispatchReceiverTarget = resolvedCall.dispatchReceiver?.getReceiverTargetDescriptor(bindingContext) val extensionReceiverTarget = resolvedCall.extensionReceiver?.getReceiverTargetDescriptor(bindingContext) diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt new file mode 100644 index 00000000000..0d8ee2f1306 --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +fun foo() { + "".apply { + -> + println(this) + } +} diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt.after b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt.after new file mode 100644 index 00000000000..c5adabd7f1d --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +fun foo() { + "".also { + println(it) + } +} diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt new file mode 100644 index 00000000000..f6fce710184 --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +fun foo() { + "".apply { + println(this + this) + } +} diff --git a/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt.after b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt.after new file mode 100644 index 00000000000..cc8f5a4d002 --- /dev/null +++ b/idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// FIX: Convert to 'also' + +fun foo() { + "".also { + println(it + it) + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index f84401d870b..f94fb23b9a8 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -3653,6 +3653,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("arrow.kt") + public void testArrow() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/arrow.kt"); + doTest(fileName); + } + @TestMetadata("doubleNestedLambdas.kt") public void testDoubleNestedLambdas() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/doubleNestedLambdas.kt"); @@ -3689,6 +3695,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + @TestMetadata("thisInOperation.kt") + public void testThisInOperation() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisInOperation.kt"); + doTest(fileName); + } + @TestMetadata("thisQualifier.kt") public void testThisQualifier() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/scopeFunctions/applyToAlso/thisQualifier.kt");