From 6ca22fbf34c72ffe4f9cb4fbb825d9d8434e89b1 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 27 Feb 2019 20:20:00 +0900 Subject: [PATCH] Remove redundant let: do not report for function stored in variable call #KT-30082 Fixed --- .../idea/intentions/ReplaceSingleLineLetIntention.kt | 4 +++- .../functionInVariableCall.kt | 7 +++++++ .../functionInVariableInvokeCall.kt | 6 ++++++ .../functionInVariableInvokeCall.kt.after | 6 ++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 10 ++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableCall.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt index a3199e6d3cc..69180ddaec7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount @@ -29,6 +30,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class ReplaceSingleLineLetInspection : IntentionBasedInspection( @@ -178,7 +180,7 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention() ?: false diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableCall.kt b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableCall.kt new file mode 100644 index 00000000000..4a50e17b69f --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableCall.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +class Foo(val bar: () -> Int) + +fun bar(foo: Foo?) { + foo?.let { it.bar() } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt new file mode 100644 index 00000000000..b28c8b999dc --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Foo(val bar: () -> Int) + +fun bar(foo: Foo?) { + foo?.let { it.bar.invoke() } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt.after b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt.after new file mode 100644 index 00000000000..78b9bc06b0b --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Foo(val bar: () -> Int) + +fun bar(foo: Foo?) { + foo?.bar?.invoke() +} \ 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 c15d15ec903..c5e3bca984a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -15174,6 +15174,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/replaceSingleLineLetIntention/functionCallOnSafeCall.kt"); } + @TestMetadata("functionInVariableCall.kt") + public void testFunctionInVariableCall() throws Exception { + runTest("idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableCall.kt"); + } + + @TestMetadata("functionInVariableInvokeCall.kt") + public void testFunctionInVariableInvokeCall() throws Exception { + runTest("idea/testData/intentions/replaceSingleLineLetIntention/functionInVariableInvokeCall.kt"); + } + @TestMetadata("in.kt") public void testIn() throws Exception { runTest("idea/testData/intentions/replaceSingleLineLetIntention/in.kt");