"Nested lambda has shadowed implicit parameter": minor improvements

This commit is contained in:
Toshiaki Kameyama
2018-08-30 15:07:46 +09:00
committed by Vyacheslav Gerasimov
parent b7c4248524
commit fba539debf
3 changed files with 19 additions and 28 deletions
@@ -5,10 +5,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElementVisitor
@@ -29,16 +26,18 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
val context = lambda.analyze(BodyResolveMode.PARTIAL)
val implicitParameter = lambda.functionDescriptor(context)?.valueParameters?.singleOrNull() ?: return
if (lambda.getParentImplicitParameterLambda(context) == null) return
val implicitParameterReference = lambda.findDescendantOfType<KtNameReferenceExpression> {
it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter
} ?: return
holder.registerProblem(
implicitParameterReference,
"Implicit parameter 'it' of enclosing lambda is shadowed",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
AddExplicitParameterToOuterLambdaFix(),
RenameImplicitParameterFix()
)
val containingFile = lambda.containingFile
lambda.forEachDescendantOfType<KtNameReferenceExpression> {
if (it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter) {
holder.registerProblem(
it,
"Implicit parameter 'it' of enclosing lambda is shadowed",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
AddExplicitParameterToOuterLambdaFix(),
IntentionWrapper(ReplaceItWithExplicitFunctionLiteralParamIntention(), containingFile)
)
}
}
})
}
@@ -60,18 +59,6 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
}
}
private class RenameImplicitParameterFix : LocalQuickFix {
override fun getName() = "Rename 'it'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val implicitParameterReference = descriptor.psiElement as? KtNameReferenceExpression ?: return
val editor = implicitParameterReference.findExistingEditor() ?: return
ReplaceItWithExplicitFunctionLiteralParamIntention().applyTo(implicitParameterReference, editor)
}
}
}
private fun KtLambdaExpression.functionDescriptor(context: BindingContext) = context[BindingContext.FUNCTION, functionLiteral]
@@ -1,4 +1,4 @@
// FIX: Rename 'it'
// FIX: Replace 'it' with explicit parameter
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
@@ -6,6 +6,8 @@ fun bar(s: String) {}
fun test() {
foo {
foo {
bar(it)
bar(it)
bar(it<caret>)
}
}
@@ -1,4 +1,4 @@
// FIX: Rename 'it'
// FIX: Replace 'it' with explicit parameter
fun foo(f: (String) -> Unit) {}
fun bar(s: String) {}
@@ -7,6 +7,8 @@ fun test() {
foo {
foo { it1 ->
bar(it1)
bar(it1)
bar(it1)
}
}
}