From 41e58402989c20b21aafe100e1cfe7a559714079 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Sat, 22 Jul 2017 17:10:19 +0200 Subject: [PATCH] Implement inspection for redundant lambda arrow Fixes #KT-11991 --- .../RedundantLambdaArrow.html | 5 ++ idea/src/META-INF/plugin.xml | 9 +++ .../RedundantLambdaArrowInspection.kt | 55 +++++++++++++++++++ .../redundantLambdaArrow/.inspection | 1 + .../redundantLambdaArrow/hasArguments.kt | 7 +++ .../redundantLambdaArrow/simple.kt | 5 ++ .../redundantLambdaArrow/simple.kt.after | 5 ++ .../LocalInspectionTestGenerated.java | 21 +++++++ 8 files changed, 108 insertions(+) create mode 100644 idea/resources/inspectionDescriptions/RedundantLambdaArrow.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/.inspection create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt.after diff --git a/idea/resources/inspectionDescriptions/RedundantLambdaArrow.html b/idea/resources/inspectionDescriptions/RedundantLambdaArrow.html new file mode 100644 index 00000000000..e5894905be8 --- /dev/null +++ b/idea/resources/inspectionDescriptions/RedundantLambdaArrow.html @@ -0,0 +1,5 @@ + + +This inspection reports lambdas without parameters that use the lambda arrow. + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 6b449c86b47..705bc2a6923 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -2446,6 +2446,15 @@ language="kotlin" /> + + diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt new file mode 100644 index 00000000000..f4d46d0458f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.inspections + +import com.intellij.codeInsight.FileModificationService +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.PsiElementVisitor +import org.jetbrains.kotlin.psi.KtLambdaExpression +import org.jetbrains.kotlin.psi.KtVisitorVoid + +class RedundantLambdaArrowInspection : AbstractKotlinInspection() { + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { + return object : KtVisitorVoid() { + override fun visitLambdaExpression(lambdaExpression: KtLambdaExpression) { + val functionLiteral = lambdaExpression.functionLiteral + if (functionLiteral.valueParameters.isNotEmpty()) return + val arrow = functionLiteral.arrow ?: return + + holder.registerProblem( + arrow, + "Redundant lambda arrow", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + DeleteFix()) + } + } + } + + class DeleteFix : LocalQuickFix { + override fun getFamilyName() = "Remove arrow" + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + val element = descriptor.psiElement + FileModificationService.getInstance().preparePsiElementForWrite(element) + element.delete() + } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/.inspection b/idea/testData/inspectionsLocal/redundantLambdaArrow/.inspection new file mode 100644 index 00000000000..d8216f258e0 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.RedundantLambdaArrowInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt new file mode 100644 index 00000000000..3814e1264b3 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt @@ -0,0 +1,7 @@ +// PROBLEM: none + +fun foo(f: (Int) -> Unit) {} + +fun bar() { + foo { i -> } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt new file mode 100644 index 00000000000..4b527a4bfc7 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/simple.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/simple.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt.after new file mode 100644 index 00000000000..2f5053d30e2 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/simple.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 f4e5c5a650b..12a87032338 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1305,6 +1305,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/redundantLambdaArrow") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RedundantLambdaArrow extends AbstractLocalInspectionTest { + public void testAllFilesPresentInRedundantLambdaArrow() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantLambdaArrow"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("hasArguments.kt") + public void testHasArguments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)