RedundantLetInspection: fix false negative for references
#KT-34603 Fixed
This commit is contained in:
@@ -59,6 +59,7 @@ abstract class RedundantLetInspection : AbstractApplicabilityBasedInspection<KtC
|
||||
is KtDotQualifiedExpression -> 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()
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val a = 42.let<caret> { t -> t }.also { println(it) }
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val a = 42.also { println(it) }
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val a = 42.let<caret> { it }
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val a = 42
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun Int.foo() {
|
||||
let<caret> { it }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun Int.foo() {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun Int.foo() {
|
||||
let<caret> { it }.also(::println)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun Int.foo() {
|
||||
also(::println)
|
||||
}
|
||||
+20
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user