From b5ba47569646f2f85078fc15fc49bf1be3b445d2 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 8 Jun 2018 11:20:12 +0900 Subject: [PATCH] "Redundant lambda arrow" inspection: Also report arrow with single underscore parameter #KT-24728 Fixed --- .../RedundantLambdaArrowInspection.kt | 24 +++++++++++++++---- .../redundantLambdaArrow/underscore.kt | 5 ++++ .../redundantLambdaArrow/underscore.kt.after | 5 ++++ .../LocalInspectionTestGenerated.java | 5 ++++ 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt index 534e374b3fe..d449894174e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -11,21 +11,34 @@ 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.util.TextRange import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.psi.KtFunctionLiteral import org.jetbrains.kotlin.psi.lambdaExpressionVisitor +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore class RedundantLambdaArrowInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return lambdaExpressionVisitor { lambdaExpression -> val functionLiteral = lambdaExpression.functionLiteral - if (functionLiteral.valueParameters.isNotEmpty()) return@lambdaExpressionVisitor val arrow = functionLiteral.arrow ?: return@lambdaExpressionVisitor + val parameters = functionLiteral.valueParameters + val singleParameter = parameters.singleOrNull() + if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true) return@lambdaExpressionVisitor + val startOffset = functionLiteral.startOffset holder.registerProblem( - arrow, + holder.manager.createProblemDescriptor( + functionLiteral, + TextRange((singleParameter?.startOffset ?: arrow.startOffset) - startOffset, arrow.endOffset - startOffset), "Redundant lambda arrow", ProblemHighlightType.LIKE_UNUSED_SYMBOL, - DeleteFix()) + isOnTheFly, + DeleteFix() + ) + ) } } @@ -33,9 +46,10 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() { override fun getFamilyName() = "Remove arrow" override fun applyFix(project: Project, descriptor: ProblemDescriptor) { - val element = descriptor.psiElement + val element = descriptor.psiElement as? KtFunctionLiteral ?: return FileModificationService.getInstance().preparePsiElementForWrite(element) - element.delete() + element.valueParameterList?.delete() + element.arrow?.delete() } } } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt new file mode 100644 index 00000000000..dfaa06f2fae --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt @@ -0,0 +1,5 @@ +fun foo(f: () -> Unit) {} + +fun bar() { + foo { _ -> } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after new file mode 100644 index 00000000000..2f5053d30e2 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after @@ -0,0 +1,5 @@ +fun foo(f: () -> Unit) {} + +fun bar() { + foo { } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 2baa377b849..c134ebd2267 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -3969,6 +3969,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testSimple() throws Exception { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt"); } + + @TestMetadata("underscore.kt") + public void testUnderscore() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt"); + } } @TestMetadata("idea/testData/inspectionsLocal/redundantObjectTypeCheck")