"Nested lambda has shadowed implicit parameter": show warning on the shadowing 'it' reference

This commit is contained in:
Toshiaki Kameyama
2018-08-29 10:36:49 +09:00
committed by Vyacheslav Gerasimov
parent 7fdbcda91d
commit 97150cb0e2
9 changed files with 27 additions and 16 deletions
@@ -28,12 +28,11 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
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
val implicitParameterReference = lambda.findDescendantOfType<KtNameReferenceExpression> {
it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter
} ?: return
holder.registerProblem(
callee,
implicitParameterReference,
"Implicit parameter 'it' of enclosing lambda is shadowed",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
AddExplicitParameterNameFix()
@@ -47,7 +46,9 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val parentLambda = (descriptor.psiElement as? KtExpression)?.getParentImplicitParameterLambda() ?: return
val implicitParameterReference = descriptor.psiElement as? KtNameReferenceExpression ?: return
val lambda = implicitParameterReference.getStrictParentOfType<KtLambdaExpression>() ?: return
val parentLambda = lambda.getParentImplicitParameterLambda() ?: return
val parameter = parentLambda.functionLiteral.getOrCreateParameterList().addParameterBefore(
KtPsiFactory(project).createLambdaParameterList("it").parameters.first(), null
)
@@ -1,10 +1,12 @@
// PROBLEM: none
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo {
<caret>foo { s ->
foo { s ->
bar(it<caret>)
}
}
}
@@ -1,10 +1,12 @@
// PROBLEM: none
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo { s ->
<caret>foo {
foo {
bar(it<caret>)
}
}
}
@@ -3,8 +3,8 @@ fun bar(s: String) {}
fun test() {
foo {
<caret>foo {
bar(it)
foo {
bar(it<caret>)
}
}
}
@@ -4,8 +4,8 @@ fun bar(f: (Int) -> Unit) {}
fun test() {
foo {
val s: String = it
<caret>bar {
val i: Int = it
bar {
val i: Int = it<caret>
}
}
}
@@ -4,8 +4,8 @@ fun bar(s: String) {}
fun test() {
foo {
foo { s ->
<caret>foo {
bar(it)
foo {
bar(it<caret>)
}
}
}
@@ -1,10 +1,12 @@
// PROBLEM: none
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() {
foo {
<caret>foo {
foo {
bar(""<caret>)
}
}
}
@@ -2,10 +2,12 @@
fun foo(f: (String) -> Unit) {}
fun bar(f: String.() -> Unit) {}
fun baz(s: String) {}
fun test() {
foo {
<caret>bar {
baz(it<caret>)
}
}
}
@@ -2,10 +2,12 @@
fun foo(f: (String) -> Unit) {}
fun bar(f: String.() -> Unit) {}
fun baz(s: String) {}
fun test() {
bar {
<caret>foo {
foo {
baz(it<caret>)
}
}
}