diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt index c77218f0ba3..725f32695f7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt @@ -47,17 +47,19 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseInten if (parameter.typeReference != null) return false if (functionLiteral.anyDescendantOfType() { literal -> - literal !== functionLiteral && - !literal.hasParameterSpecification() && - literal.anyDescendantOfType { nameExpr -> - nameExpr.getReferencedName() == element.text - } + literal.usesName(element.text) && + (!literal.hasParameterSpecification() || literal.usesName("it")) } ) return false text = "Replace explicit parameter '${parameter.name}' with 'it'" return true } + private fun KtFunctionLiteral.usesName(name: String): Boolean = + anyDescendantOfType { + nameExpr -> nameExpr.getReferencedName() == name + } + override fun startInWriteAction(): Boolean = false override fun invoke(project: Project, editor: Editor, element: PsiElement) { diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt new file mode 100644 index 00000000000..0447495a1f2 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +// See KT-12437: it from outer lambda +fun acceptLambda(p: Int, f: (Int) -> Int): Int = f(p) +fun use() { + acceptLambda(1) { + acceptLambda(2) { p2 -> it * 10 + p2 } + }.hashCode() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index bcb760eea47..dbfd90b8314 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8293,6 +8293,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("notApplicable_itFromOuterLambda.kt") + public void testNotApplicable_itFromOuterLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt"); + doTest(fileName); + } + @TestMetadata("notApplicable_nestedLiterals.kt") public void testNotApplicable_nestedLiterals() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiterals.kt");