Remove redundant let: do not report for function stored in variable call

#KT-30082 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-27 20:20:00 +09:00
committed by Mikhail Glukhikh
parent 304007f602
commit 6ca22fbf34
5 changed files with 32 additions and 1 deletions
@@ -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<KtCallExpression>(
@@ -178,7 +180,7 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
receiver is KtNameReferenceExpression &&
receiver.getReferencedName() == parameterName &&
!nameUsed(parameterName, except = receiver)
}
} && callExpression?.resolveToCall() !is VariableAsFunctionResolvedCall
private fun KtDotQualifiedExpression.hasLambdaExpression() = selectorExpression?.anyDescendantOfType<KtLambdaExpression>() ?: false
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
class Foo(val bar: () -> Int)
fun bar(foo: Foo?) {
foo?.<caret>let { it.bar() }
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
class Foo(val bar: () -> Int)
fun bar(foo: Foo?) {
foo?.<caret>let { it.bar.invoke() }
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
class Foo(val bar: () -> Int)
fun bar(foo: Foo?) {
foo?.bar?.invoke()
}
@@ -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");