KT-11850 Add nested lambdas with implicit parameters warning (#1664)
* Add `Nested lambda has shadowed implicit parameter` inspection #KT-11850 Fixed * KT-11850 Fixed
This commit is contained in:
committed by
Vyacheslav Gerasimov
parent
c0f53f3a4d
commit
e41a34af88
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports a nested lambda with shadowed implicit parameter.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2882,6 +2882,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2880,6 +2880,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2880,6 +2880,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2881,6 +2881,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2880,6 +2880,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2880,6 +2880,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection"
|
||||
displayName="Nested lambda has shadowed implicit parameter"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
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.openapi.project.Project
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
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.resolve.BindingContext
|
||||
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.getParentImplicitParameterLambda(context) == null) return
|
||||
val callee = lambda.getStrictParentOfType<KtCallExpression>()?.calleeExpression ?: return
|
||||
holder.registerProblem(
|
||||
callee,
|
||||
"Implicit parameter 'it' of enclosing lambda is shadowed",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
AddExplicitParameterNameFix()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private class AddExplicitParameterNameFix : LocalQuickFix {
|
||||
override fun getName() = "Add explicit parameter name to outer lambda"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val parentLambda = (descriptor.psiElement as? KtExpression)?.getParentImplicitParameterLambda() ?: return
|
||||
val parameter = parentLambda.functionLiteral.getOrCreateParameterList().addParameterBefore(
|
||||
KtPsiFactory(project).createLambdaParameterList("it").parameters.first(), null
|
||||
)
|
||||
val editor = parentLambda.findExistingEditor()
|
||||
if (editor != null) {
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
||||
editor.caretModel.moveToOffset(parameter.startOffset)
|
||||
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtLambdaExpression.functionDescriptor(context: BindingContext) = context[BindingContext.FUNCTION, functionLiteral]
|
||||
|
||||
private fun KtExpression.getParentImplicitParameterLambda(
|
||||
context: BindingContext = this.analyze(BodyResolveMode.PARTIAL)
|
||||
): KtLambdaExpression? {
|
||||
return getParentOfTypesAndPredicate(true, KtLambdaExpression::class.java) {
|
||||
it.valueParameters.isEmpty() && it.functionDescriptor(context)?.valueParameters?.size == 1
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.NestedLambdaShadowedImplicitParameterInspection
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
<caret>foo { s ->
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo { s ->
|
||||
<caret>foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
<caret>foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo { it ->
|
||||
foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: (Int) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
val s: String = it
|
||||
<caret>bar {
|
||||
val i: Int = it
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: (Int) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo { it ->
|
||||
val s: String = it
|
||||
bar {
|
||||
val i: Int = it
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
foo { s ->
|
||||
<caret>foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo { it ->
|
||||
foo { s ->
|
||||
foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: String.() -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
foo {
|
||||
<caret>bar {
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(f: (String) -> Unit) {}
|
||||
fun bar(f: String.() -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
bar {
|
||||
<caret>foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -2814,6 +2814,54 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class NestedLambdaShadowedImplicitParameter extends AbstractLocalInspectionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInNestedLambdaShadowedImplicitParameter() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("explicit.kt")
|
||||
public void testExplicit() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/explicit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("explicitParent.kt")
|
||||
public void testExplicitParent() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/explicitParent.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implicit.kt")
|
||||
public void testImplicit() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implicit2.kt")
|
||||
public void testImplicit2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("implicitGrandParent.kt")
|
||||
public void testImplicitGrandParent() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/implicitGrandParent.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("receiver.kt")
|
||||
public void testReceiver() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/receiver.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("receiverParent.kt")
|
||||
public void testReceiverParent() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/nestedLambdaShadowedImplicitParameter/receiverParent.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/nullChecksToSafeCall")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user