RedundantLetInspection: fix false positive for inner lambda expression

#KT-25271 Fixed
This commit is contained in:
Dmitry Gridin
2019-10-25 22:10:28 +07:00
parent 5d16753285
commit 5fc70f6cfd
4 changed files with 34 additions and 7 deletions
@@ -17,10 +17,7 @@ import org.jetbrains.kotlin.idea.intentions.*
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount
import org.jetbrains.kotlin.idea.util.textRangeIn
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.getReferenceTargets
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
@@ -88,9 +85,11 @@ class ComplexRedundantLetInspection : RedundantLetInspection() {
if (element.parent is KtSafeQualifiedExpression) {
false
} else {
val count = lambdaExpression.functionLiteral.valueParameterReferences(bodyExpression).count()
val references = lambdaExpression.functionLiteral.valueParameterReferences(bodyExpression)
val destructuringDeclaration = lambdaExpression.functionLiteral.valueParameters.firstOrNull()?.destructuringDeclaration
count == 0 || (count == 1 && destructuringDeclaration == null)
references.isEmpty() || (references.singleOrNull()?.takeIf { expression ->
expression.parents.takeWhile { it != lambdaExpression.functionLiteral }.find { it is KtFunction } == null
} != null && destructuringDeclaration == null)
}
else ->
false
@@ -229,7 +228,7 @@ private fun isSingleLine(element: KtCallExpression): Boolean {
var count = 1
while (true) {
if (count > 2) return false
receiver = (receiver as? KtQualifiedExpression)?.receiverExpression ?: break
receiver = (receiver as? KtQualifiedExpression)?.receiverExpression ?: break
count++
}
return true
@@ -0,0 +1,11 @@
// PROBLEM: none
// WITH_RUNTIME
val a = randomValue().let<caret> { r ->
List(10, fun(it: Int): Int {
return r
})
}
fun randomValue(): Int = 42
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
val a = randomValue().let<caret> { r -> List(10) { r } }
fun randomValue(): Int = 42
@@ -2436,6 +2436,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testTypeChecks() throws Exception {
runTest("idea/testData/inspectionsLocal/complexRedundantLet/typeChecks.kt");
}
@TestMetadata("withInnerFunction.kt")
public void testWithInnerFunction() throws Exception {
runTest("idea/testData/inspectionsLocal/complexRedundantLet/withInnerFunction.kt");
}
@TestMetadata("withInnerLambda.kt")
public void testWithInnerLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/complexRedundantLet/withInnerLambda.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/constantConditionIf")