From 13e98e712e44875321ef0545b02fe26f6deb6ff7 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Fri, 25 Oct 2019 22:26:03 +0700 Subject: [PATCH] RedundantLetInspection: fix false negative for references #KT-34603 Fixed --- .../inspections/RedundantLetInspection.kt | 17 +++++++++++++++- .../simpleRedundantLet/reference.kt | 3 +++ .../simpleRedundantLet/reference.kt.after | 3 +++ .../simpleRedundantLet/reference2.kt | 3 +++ .../simpleRedundantLet/reference2.kt.after | 3 +++ .../simpleRedundantLet/reference3.kt | 5 +++++ .../simpleRedundantLet/reference3.kt.after | 4 ++++ .../simpleRedundantLet/reference4.kt | 5 +++++ .../simpleRedundantLet/reference4.kt.after | 5 +++++ .../LocalInspectionTestGenerated.java | 20 +++++++++++++++++++ 10 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt.after create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt.after create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt.after create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt create mode 100644 idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt index 03e89bfc3d4..62912b6d880 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLetInspection.kt @@ -59,6 +59,7 @@ abstract class RedundantLetInspection : AbstractApplicabilityBasedInspection bodyExpression.applyTo(element) is KtBinaryExpression -> bodyExpression.applyTo(element) is KtCallExpression -> bodyExpression.applyTo(element, lambdaExpression.functionLiteral, editor) + is KtSimpleNameExpression -> deleteCall(element) } } } @@ -69,7 +70,11 @@ class SimpleRedundantLetInspection : RedundantLetInspection() { bodyExpression: PsiElement, lambdaExpression: KtLambdaExpression, parameterName: String - ): Boolean = if (bodyExpression is KtDotQualifiedExpression) bodyExpression.isApplicable(parameterName) else false + ): Boolean = when (bodyExpression) { + is KtDotQualifiedExpression -> bodyExpression.isApplicable(parameterName) + is KtSimpleNameExpression -> bodyExpression.text == parameterName + else -> false + } } class ComplexRedundantLetInspection : RedundantLetInspection() { @@ -138,6 +143,16 @@ private fun KtDotQualifiedExpression.applyTo(element: KtCallExpression) { } } +private fun deleteCall(element: KtCallExpression) { + val parent = element.parent as? KtDotQualifiedExpression + if (parent != null) { + val replacement = parent.selectorExpression?.takeIf { it != element } ?: parent.receiverExpression + parent.replace(replacement) + } else { + element.delete() + } +} + private fun KtCallExpression.applyTo(element: KtCallExpression, functionLiteral: KtFunctionLiteral, editor: Editor?) { val parent = element.parent as? KtQualifiedExpression val reference = functionLiteral.valueParameterReferences(this).firstOrNull() diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt b/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt new file mode 100644 index 00000000000..9890aa035c6 --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val a = 42.let { t -> t }.also { println(it) } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt.after b/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt.after new file mode 100644 index 00000000000..346330fd478 --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val a = 42.also { println(it) } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt b/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt new file mode 100644 index 00000000000..71499365cc1 --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val a = 42.let { it } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt.after b/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt.after new file mode 100644 index 00000000000..1c2eaca295a --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val a = 42 \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt b/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt new file mode 100644 index 00000000000..1968bfbc67d --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun Int.foo() { + let { it } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt.after b/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt.after new file mode 100644 index 00000000000..6dfdde1a24a --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME + +fun Int.foo() { +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt b/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt new file mode 100644 index 00000000000..b820b275bb3 --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun Int.foo() { + let { it }.also(::println) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt.after b/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt.after new file mode 100644 index 00000000000..718f7a48dae --- /dev/null +++ b/idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun Int.foo() { + also(::println) +} \ 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 9ac4dc33ba0..476f45e2fff 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -11650,6 +11650,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/simpleRedundantLet/receiverWithLambda2.kt"); } + @TestMetadata("reference.kt") + public void testReference() throws Exception { + runTest("idea/testData/inspectionsLocal/simpleRedundantLet/reference.kt"); + } + + @TestMetadata("reference2.kt") + public void testReference2() throws Exception { + runTest("idea/testData/inspectionsLocal/simpleRedundantLet/reference2.kt"); + } + + @TestMetadata("reference3.kt") + public void testReference3() throws Exception { + runTest("idea/testData/inspectionsLocal/simpleRedundantLet/reference3.kt"); + } + + @TestMetadata("reference4.kt") + public void testReference4() throws Exception { + runTest("idea/testData/inspectionsLocal/simpleRedundantLet/reference4.kt"); + } + @TestMetadata("sameLets.kt") public void testSameLets() throws Exception { runTest("idea/testData/inspectionsLocal/simpleRedundantLet/sameLets.kt");