From 2a7eb2fb0e388a97ccd19dab409c640fc0bb3054 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 3 Feb 2019 00:28:04 +0900 Subject: [PATCH] Redundant lambda arrow: do not report on nested lambda #KT-29093 Fixed --- .../inspections/RedundantLambdaArrowInspection.kt | 9 +++++++++ .../redundantLambdaArrow/nestedLambda.kt | 7 +++++++ .../redundantLambdaArrow/nestedLambda2.kt | 9 +++++++++ .../redundantLambdaArrow/nestedLambda3.kt | 9 +++++++++ .../redundantLambdaArrow/nestedLambda3.kt.after | 9 +++++++++ .../inspections/LocalInspectionTestGenerated.java | 15 +++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt index afff0f7f0ca..07cdde274c8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -16,7 +16,9 @@ import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall +import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.startOffset @@ -59,6 +61,13 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() { if (argumentMatch?.valueParameter?.original?.type?.isTypeParameter() == true) return } + val functionLiteralDescriptor = functionLiteral.descriptor + if (functionLiteralDescriptor != null) { + if (functionLiteral.anyDescendantOfType { + it.text == "it" && it.resolveToCall()?.resultingDescriptor?.containingDeclaration != functionLiteralDescriptor + }) return + } + val startOffset = functionLiteral.startOffset holder.registerProblem( holder.manager.createProblemDescriptor( diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt new file mode 100644 index 00000000000..e4f80c8b3f5 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test() { + listOf("A").forEach { + setOf(1).map { _ -> it.length } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt new file mode 100644 index 00000000000..649cfcbfd49 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test() { + listOf("A").forEach { + listOf("B").forEach { _ -> + setOf(1).map { _ -> it.length } + } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt new file mode 100644 index 00000000000..b335048e90a --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +fun test() { + listOf("A").forEach { + setOf(1).map { _ -> + val it = "B" + it.length + } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt.after new file mode 100644 index 00000000000..0b7fcfbede2 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +fun test() { + listOf("A").forEach { + setOf(1).map { + val it = "B" + it.length + } + } +} \ 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 1aa13b2a769..2a1de9df46c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5009,6 +5009,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt"); } + @TestMetadata("nestedLambda.kt") + public void testNestedLambda() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt"); + } + + @TestMetadata("nestedLambda2.kt") + public void testNestedLambda2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt"); + } + + @TestMetadata("nestedLambda3.kt") + public void testNestedLambda3() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt"); + } + @TestMetadata("noExpectedType.kt") public void testNoExpectedType() throws Exception { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/noExpectedType.kt");