"Nested lambda has shadowed implicit parameter": minor improvements
This commit is contained in:
committed by
Vyacheslav Gerasimov
parent
b7c4248524
commit
fba539debf
+13
-26
@@ -5,10 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
import com.intellij.codeInspection.LocalQuickFix
|
import com.intellij.codeInspection.*
|
||||||
import com.intellij.codeInspection.ProblemDescriptor
|
|
||||||
import com.intellij.codeInspection.ProblemHighlightType
|
|
||||||
import com.intellij.codeInspection.ProblemsHolder
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
@@ -29,16 +26,18 @@ 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
|
||||||
val implicitParameterReference = lambda.findDescendantOfType<KtNameReferenceExpression> {
|
val containingFile = lambda.containingFile
|
||||||
it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter
|
lambda.forEachDescendantOfType<KtNameReferenceExpression> {
|
||||||
} ?: return
|
if (it.text == "it" && it.getResolvedCall(context)?.resultingDescriptor == implicitParameter) {
|
||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
implicitParameterReference,
|
it,
|
||||||
"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,
|
||||||
AddExplicitParameterToOuterLambdaFix(),
|
AddExplicitParameterToOuterLambdaFix(),
|
||||||
RenameImplicitParameterFix()
|
IntentionWrapper(ReplaceItWithExplicitFunctionLiteralParamIntention(), containingFile)
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,18 +59,6 @@ class NestedLambdaShadowedImplicitParameterInspection : AbstractKotlinInspection
|
|||||||
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
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]
|
private fun KtLambdaExpression.functionDescriptor(context: BindingContext) = context[BindingContext.FUNCTION, functionLiteral]
|
||||||
|
|||||||
+3
-1
@@ -1,4 +1,4 @@
|
|||||||
// FIX: Rename 'it'
|
// FIX: Replace 'it' with explicit parameter
|
||||||
|
|
||||||
fun foo(f: (String) -> Unit) {}
|
fun foo(f: (String) -> Unit) {}
|
||||||
fun bar(s: String) {}
|
fun bar(s: String) {}
|
||||||
@@ -6,6 +6,8 @@ fun bar(s: String) {}
|
|||||||
fun test() {
|
fun test() {
|
||||||
foo {
|
foo {
|
||||||
foo {
|
foo {
|
||||||
|
bar(it)
|
||||||
|
bar(it)
|
||||||
bar(it<caret>)
|
bar(it<caret>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -1,4 +1,4 @@
|
|||||||
// FIX: Rename 'it'
|
// FIX: Replace 'it' with explicit parameter
|
||||||
|
|
||||||
fun foo(f: (String) -> Unit) {}
|
fun foo(f: (String) -> Unit) {}
|
||||||
fun bar(s: String) {}
|
fun bar(s: String) {}
|
||||||
@@ -7,6 +7,8 @@ fun test() {
|
|||||||
foo {
|
foo {
|
||||||
foo { it1 ->
|
foo { it1 ->
|
||||||
bar(it1)
|
bar(it1)
|
||||||
|
bar(it1)
|
||||||
|
bar(it1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user