Redundant lambda arrow: do not report on nested lambda

#KT-29093 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-03 00:28:04 +09:00
committed by Mikhail Glukhikh
parent bf7f7c81b1
commit 2a7eb2fb0e
6 changed files with 58 additions and 0 deletions
@@ -16,7 +16,9 @@ import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -59,6 +61,13 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() {
if (argumentMatch?.valueParameter?.original?.type?.isTypeParameter() == true) return
}
val functionLiteralDescriptor = functionLiteral.descriptor
if (functionLiteralDescriptor != null) {
if (functionLiteral.anyDescendantOfType<KtNameReferenceExpression> {
it.text == "it" && it.resolveToCall()?.resultingDescriptor?.containingDeclaration != functionLiteralDescriptor
}) return
}
val startOffset = functionLiteral.startOffset
holder.registerProblem(
holder.manager.createProblemDescriptor(
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
listOf("A").forEach {
setOf(1).map { <caret>_ -> it.length }
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
listOf("A").forEach {
listOf("B").forEach { _ ->
setOf(1).map { <caret>_ -> it.length }
}
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
listOf("A").forEach {
setOf(1).map { <caret>_ ->
val it = "B"
it.length
}
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
listOf("A").forEach {
setOf(1).map {
val it = "B"
it.length
}
}
}
@@ -5009,6 +5009,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt");
}
@TestMetadata("nestedLambda.kt")
public void testNestedLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda.kt");
}
@TestMetadata("nestedLambda2.kt")
public void testNestedLambda2() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda2.kt");
}
@TestMetadata("nestedLambda3.kt")
public void testNestedLambda3() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/nestedLambda3.kt");
}
@TestMetadata("noExpectedType.kt")
public void testNoExpectedType() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/noExpectedType.kt");