From 5fc70f6cfd10b22317731dfa157e2ee0b80f02f1 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Fri, 25 Oct 2019 22:10:28 +0700 Subject: [PATCH] RedundantLetInspection: fix false positive for inner lambda expression #KT-25271 Fixed --- .../idea/inspections/RedundantLetInspection.kt | 13 ++++++------- .../complexRedundantLet/withInnerFunction.kt | 11 +++++++++++ .../complexRedundantLet/withInnerLambda.kt | 7 +++++++ .../inspections/LocalInspectionTestGenerated.java | 10 ++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt create mode 100644 idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt index 5335f9aca2c..03e89bfc3d4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt @@ -17,10 +17,7 @@ import org.jetbrains.kotlin.idea.intentions.* import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount import org.jetbrains.kotlin.idea.util.textRangeIn import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType -import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType -import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector -import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.bindingContextUtil.getReferenceTargets import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall @@ -88,9 +85,11 @@ class ComplexRedundantLetInspection : RedundantLetInspection() { if (element.parent is KtSafeQualifiedExpression) { false } else { - val count = lambdaExpression.functionLiteral.valueParameterReferences(bodyExpression).count() + val references = lambdaExpression.functionLiteral.valueParameterReferences(bodyExpression) val destructuringDeclaration = lambdaExpression.functionLiteral.valueParameters.firstOrNull()?.destructuringDeclaration - count == 0 || (count == 1 && destructuringDeclaration == null) + references.isEmpty() || (references.singleOrNull()?.takeIf { expression -> + expression.parents.takeWhile { it != lambdaExpression.functionLiteral }.find { it is KtFunction } == null + } != null && destructuringDeclaration == null) } else -> false @@ -229,7 +228,7 @@ private fun isSingleLine(element: KtCallExpression): Boolean { var count = 1 while (true) { if (count > 2) return false - receiver = (receiver as? KtQualifiedExpression)?.receiverExpression ?: break + receiver = (receiver as? KtQualifiedExpression)?.receiverExpression ?: break count++ } return true diff --git a/idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt b/idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt new file mode 100644 index 00000000000..845ed8ccce6 --- /dev/null +++ b/idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt @@ -0,0 +1,11 @@ +// PROBLEM: none +// WITH_RUNTIME + + +val a = randomValue().let { r -> + List(10, fun(it: Int): Int { + return r + }) +} + +fun randomValue(): Int = 42 \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt b/idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt new file mode 100644 index 00000000000..899ffb7cb03 --- /dev/null +++ b/idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +// WITH_RUNTIME + + +val a = randomValue().let { r -> List(10) { r } } + +fun randomValue(): Int = 42 \ 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 669ab37a695..9ac4dc33ba0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -2436,6 +2436,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testTypeChecks() throws Exception { runTest("idea/testData/inspectionsLocal/complexRedundantLet/typeChecks.kt"); } + + @TestMetadata("withInnerFunction.kt") + public void testWithInnerFunction() throws Exception { + runTest("idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt"); + } + + @TestMetadata("withInnerLambda.kt") + public void testWithInnerLambda() throws Exception { + runTest("idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt"); + } } @TestMetadata("idea/testData/inspectionsLocal/constantConditionIf")