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")