"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 context = lambda.analyze(BodyResolveMode.PARTIAL)
val implicitParameter = lambda.functionDescriptor(context)?.valueParameters?.singleOrNull() ?: return val implicitParameter = lambda.functionDescriptor(context)?.valueParameters?.singleOrNull() ?: return
if (lambda.getParentImplicitParameterLambda(context) == null) return if (lambda.getParentImplicitParameterLambda(context) == null) return
if (lambda.findDescendantOfType<KtNameReferenceExpression> { val implicitParameterReference = lambda.findDescendantOfType<KtNameReferenceExpression> {
it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter
} == null) return } ?: return
val callee = lambda.getStrictParentOfType<KtCallExpression>()?.calleeExpression ?: return
holder.registerProblem( holder.registerProblem(
callee, implicitParameterReference,
"Implicit parameter 'it' of enclosing lambda is shadowed", "Implicit parameter 'it' of enclosing lambda is shadowed",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
AddExplicitParameterNameFix() AddExplicitParameterNameFix()
@@ -47,7 +46,9 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
override fun getFamilyName() = name override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { 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( val parameter = parentLambda.functionLiteral.getOrCreateParameterList().addParameterBefore(
KtPsiFactory(project).createLambdaParameterList("it").parameters.first(), null KtPsiFactory(project).createLambdaParameterList("it").parameters.first(), null
) )
@@ -1,10 +1,12 @@
// PROBLEM: none // PROBLEM: none
fun foo(f: (String) -> Unit) {} fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() { fun test() {
foo { foo {
<caret>foo { s -> foo { s ->
bar(it<caret>)
} }
} }
} }
@@ -1,10 +1,12 @@
// PROBLEM: none // PROBLEM: none
fun foo(f: (String) -> Unit) {} fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() { fun test() {
foo { s -> foo { s ->
<caret>foo { foo {
bar(it<caret>)
} }
} }
} }
@@ -3,8 +3,8 @@ fun bar(s: String) {}
fun test() { fun test() {
foo { foo {
<caret>foo { foo {
bar(it) bar(it<caret>)
} }
} }
} }
@@ -4,8 +4,8 @@ fun bar(f: (Int) -> Unit) {}
fun test() { fun test() {
foo { foo {
val s: String = it val s: String = it
<caret>bar { bar {
val i: Int = it val i: Int = it<caret>
} }
} }
} }
@@ -4,8 +4,8 @@ fun bar(s: String) {}
fun test() { fun test() {
foo { foo {
foo { s -> foo { s ->
<caret>foo { foo {
bar(it) bar(it<caret>)
} }
} }
} }
@@ -1,10 +1,12 @@
// PROBLEM: none // PROBLEM: none
fun foo(f: (String) -> Unit) {} fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
fun test() { fun test() {
foo { foo {
<caret>foo { foo {
bar(""<caret>)
} }
} }
} }
@@ -2,10 +2,12 @@
fun foo(f: (String) -> Unit) {} fun foo(f: (String) -> Unit) {}
fun bar(f: String.() -> Unit) {} fun bar(f: String.() -> Unit) {}
fun baz(s: String) {}
fun test() { fun test() {
foo { foo {
<caret>bar { <caret>bar {
baz(it<caret>)
} }
} }
} }
@@ -2,10 +2,12 @@
fun foo(f: (String) -> Unit) {} fun foo(f: (String) -> Unit) {}
fun bar(f: String.() -> Unit) {} fun bar(f: String.() -> Unit) {}
fun baz(s: String) {}
fun test() { fun test() {
bar { bar {
<caret>foo { foo {
baz(it<caret>)
} }
} }
} }