RedundantLetInspection: fix false negative for references

#KT-34603 Fixed
This commit is contained in:
Dmitry Gridin
2019-10-25 22:26:03 +07:00
parent 5fc70f6cfd
commit 13e98e712e
10 changed files with 67 additions and 1 deletions
@@ -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)
}
@@ -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");