From 6b2c87020b9b497dc41451b26a11197d11d7588f Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 6 Dec 2019 14:13:43 +0100 Subject: [PATCH] Replace explicit parameter with 'it': do not suggest when lambda is directly under "when" or "if" Relates to #KT-35320 --- ...icitFunctionLiteralParamWithItIntention.kt | 5 ++++ .../applicable_InIf.kt | 4 +++ .../applicable_InIf.kt.after | 4 +++ .../applicable_InIfElse.kt | 4 +++ .../applicable_InIfElse.kt.after | 4 +++ .../applicable_inWhenEntry.kt | 9 ++++++ .../applicable_inWhenEntry.kt.after | 9 ++++++ .../notApplicable_InIf.kt | 5 ++++ .../notApplicable_InIfElse.kt | 5 ++++ .../notApplicable_inWhenEntry.kt | 10 +++++++ .../intentions/IntentionTestGenerated.java | 30 +++++++++++++++++++ 11 files changed, 89 insertions(+) create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt.after create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt.after create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt.after create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt create mode 100644 idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt index e94c5f8fdb8..1de963158a7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt @@ -33,6 +33,9 @@ import org.jetbrains.kotlin.idea.core.copied import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.* +import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode @@ -53,6 +56,8 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention : PsiElementBaseIntenti }) return false val lambda = functionLiteral.parent as? KtLambdaExpression ?: return false + val lambdaParent = lambda.parent + if (lambdaParent is KtWhenEntry || lambdaParent is KtContainerNodeForControlStructureBody) return false val call = lambda.getStrictParentOfType() if (call != null) { val argumentIndex = call.valueArguments.indexOfFirst { it.getArgumentExpression() == lambda } diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt new file mode 100644 index 00000000000..e34c4fb68fa --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt @@ -0,0 +1,4 @@ +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { { s -> true } } else { { s -> false } } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt.after b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt.after new file mode 100644 index 00000000000..3fa8805b947 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt.after @@ -0,0 +1,4 @@ +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { { true } } else { { s -> false } } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt new file mode 100644 index 00000000000..d262afa3670 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt @@ -0,0 +1,4 @@ +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { { s -> true } } else { { s -> false } } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt.after b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt.after new file mode 100644 index 00000000000..45f0be8fc3c --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt.after @@ -0,0 +1,4 @@ +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { { s -> true } } else { { false } } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt new file mode 100644 index 00000000000..f605b9892c7 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt @@ -0,0 +1,9 @@ +fun test(v: Boolean): (String) -> Int { + return when (v) { + true -> { { x -> taskOne(x) } } + false -> { x -> taskTwo(x) } + } +} + +fun taskOne(s: String) = s.length +fun taskTwo(s: String) = 42 diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt.after b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt.after new file mode 100644 index 00000000000..f238a586ea0 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt.after @@ -0,0 +1,9 @@ +fun test(v: Boolean): (String) -> Int { + return when (v) { + true -> { { taskOne(it) } } + false -> { x -> taskTwo(x) } + } +} + +fun taskOne(s: String) = s.length +fun taskTwo(s: String) = 42 diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt new file mode 100644 index 00000000000..090a5a84c71 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { s -> true } else { s -> false } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt new file mode 100644 index 00000000000..bc1fba3b387 --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false +fun test(i: Int) { + val p: (String) -> Boolean = + if (i == 1) { s -> true } else { s -> false } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt new file mode 100644 index 00000000000..55b7dcf6ebb --- /dev/null +++ b/idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt @@ -0,0 +1,10 @@ +// IS_APPLICABLE: false +fun test(v: Boolean): (String) -> Int { + return when (v) { + true -> { { x -> taskOne(x) } } + false -> { x -> taskTwo(x) } + } +} + +fun taskOne(s: String) = s.length +fun taskTwo(s: String) = 42 diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 756dc590dfe..6c41aecf52d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -14732,6 +14732,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("applicable_InIf.kt") + public void testApplicable_InIf() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIf.kt"); + } + + @TestMetadata("applicable_InIfElse.kt") + public void testApplicable_InIfElse() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_InIfElse.kt"); + } + @TestMetadata("applicable_cursofOverParamInInnerLiteral.kt") public void testApplicable_cursofOverParamInInnerLiteral() throws Exception { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_cursofOverParamInInnerLiteral.kt"); @@ -14757,6 +14767,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inPropertyInitializer.kt"); } + @TestMetadata("applicable_inWhenEntry.kt") + public void testApplicable_inWhenEntry() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_inWhenEntry.kt"); + } + @TestMetadata("applicable_nestedLiteralsNoUseInside.kt") public void testApplicable_nestedLiteralsNoUseInside() throws Exception { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_nestedLiteralsNoUseInside.kt"); @@ -14767,6 +14782,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_qualifiedExpression.kt"); } + @TestMetadata("notApplicable_InIf.kt") + public void testNotApplicable_InIf() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIf.kt"); + } + + @TestMetadata("notApplicable_InIfElse.kt") + public void testNotApplicable_InIfElse() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_InIfElse.kt"); + } + @TestMetadata("notApplicable_alreadyUsesImplicitIt.kt") public void testNotApplicable_alreadyUsesImplicitIt() throws Exception { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_alreadyUsesImplicitIt.kt"); @@ -14782,6 +14807,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_hasMultipleParameters.kt"); } + @TestMetadata("notApplicable_inWhenEntry.kt") + public void testNotApplicable_inWhenEntry() throws Exception { + runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_inWhenEntry.kt"); + } + @TestMetadata("notApplicable_itFromOuterLambda.kt") public void testNotApplicable_itFromOuterLambda() throws Exception { runTest("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_itFromOuterLambda.kt");