"Nested lambda has shadowed implicit parameter": Do not report if 'it' paramerter is not used #KT-26268 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-08-21 11:41:08 +09:00
committed by Vyacheslav Gerasimov
parent f3879af9f6
commit 7800f42840
7 changed files with 31 additions and 6 deletions
@@ -15,20 +15,22 @@ import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.refactoring.rename.KotlinVariableInplaceRenameHandler
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getOrCreateParameterList
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return lambdaExpressionVisitor(fun(lambda: KtLambdaExpression) {
val context: BindingContext by lazy { lambda.analyze(BodyResolveMode.PARTIAL) }
if (lambda.valueParameters.isNotEmpty() || lambda.functionDescriptor(context)?.valueParameters?.size != 1) return
if (lambda.valueParameters.isNotEmpty()) return
val context = lambda.analyze(BodyResolveMode.PARTIAL)
val implicitParameter = lambda.functionDescriptor(context)?.valueParameters?.singleOrNull() ?: return
if (lambda.getParentImplicitParameterLambda(context) == null) return
if (lambda.findDescendantOfType<KtNameReferenceExpression> {
it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter
} == null) return
val callee = lambda.getStrictParentOfType<KtCallExpression>()?.calleeExpression ?: return
holder.registerProblem(
callee,
@@ -1,8 +1,10 @@
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo {
<caret>foo {
bar(it)
}
}
}
@@ -1,8 +1,10 @@
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo { it ->
foo {
bar(it)
}
}
}
@@ -1,9 +1,11 @@
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo {
foo { s ->
<caret>foo {
bar(it)
}
}
}
@@ -1,9 +1,11 @@
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo { it ->
foo { s ->
foo {
bar(it)
}
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun foo(f: (String) -> Unit) {}
fun test() {
foo {
<caret>foo {
}
}
}
@@ -3471,6 +3471,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicitGrandParent.kt");
}
@TestMetadata("notUseParameter.kt")
public void testNotUseParameter() throws Exception {
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/notUseParameter.kt");
}
@TestMetadata("receiver.kt")
public void testReceiver() throws Exception {
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/receiver.kt");