From e8a8bc89c982e5132ca376da87ad3ef728f9389e Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 4 Feb 2019 12:21:35 +0900 Subject: [PATCH] Add "Redundant enum constructor invocation" inspection #KT-29321 Fixed --- idea/resources/META-INF/plugin-common.xml | 9 ++++ .../RedundantEnumConstructorInvocation.html | 14 +++++ ...dantEnumConstructorInvocationInspection.kt | 53 +++++++++++++++++++ .../.inspection | 1 + .../basic.kt | 5 ++ .../basic.kt.after | 5 ++ .../basic2.kt | 5 ++ .../basic2.kt.after | 5 ++ .../hasArgument.kt | 5 ++ .../hasError.kt | 6 +++ .../LocalInspectionTestGenerated.java | 33 ++++++++++++ 11 files changed, 141 insertions(+) create mode 100644 idea/resources/inspectionDescriptions/RedundantEnumConstructorInvocation.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEnumConstructorInvocationInspection.kt create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/.inspection create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt.after create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt.after create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasArgument.kt create mode 100644 idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasError.kt diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index 2d1cf735aea..b52f5a8201a 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -3239,6 +3239,15 @@ language="kotlin" /> + + diff --git a/idea/resources/inspectionDescriptions/RedundantEnumConstructorInvocation.html b/idea/resources/inspectionDescriptions/RedundantEnumConstructorInvocation.html new file mode 100644 index 00000000000..5c7c4b3e392 --- /dev/null +++ b/idea/resources/inspectionDescriptions/RedundantEnumConstructorInvocation.html @@ -0,0 +1,14 @@ + + +This inspection reports redundant enum entry constructor invocation: + +
+enum class Foo {
+    A(), // This '()' is redundant.
+    B(),
+    C
+}
+
+ + + \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEnumConstructorInvocationInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEnumConstructorInvocationInspection.kt new file mode 100644 index 00000000000..ed1d507f7ed --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEnumConstructorInvocationInspection.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2019 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 org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.psi.KtEnumEntry +import org.jetbrains.kotlin.psi.KtSuperTypeCallEntry +import org.jetbrains.kotlin.psi.KtValueArgumentList +import org.jetbrains.kotlin.psi.enumEntryVisitor +import org.jetbrains.kotlin.psi.psiUtil.containingClass +import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType + +class RedundantEnumConstructorInvocationInspection : AbstractKotlinInspection() { + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = enumEntryVisitor(fun(enumEntry) { + val valueArgumentList = enumEntry.valueArgumentList() ?: return + holder.registerProblem( + valueArgumentList, + "Redundant enum constructor invocation", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + RemoveEnumConstructorInvocationFix() + ) + }) +} + +private class RemoveEnumConstructorInvocationFix : LocalQuickFix { + override fun getName() = "Remove enum constructor invocation" + + override fun getFamilyName() = name + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + descriptor.psiElement.getStrictParentOfType()?.containingClass()?.body?.getChildrenOfType()?.forEach { + it.valueArgumentList()?.delete() + } + } +} + +private fun KtEnumEntry.valueArgumentList(): KtValueArgumentList? { + val superTypeCallEntry = initializerList?.initializers?.singleOrNull() as? KtSuperTypeCallEntry ?: return null + val valueArgumentList = superTypeCallEntry.valueArgumentList ?: return null + if (valueArgumentList.arguments.isNotEmpty()) return null + if (valueArgumentList.analyze().diagnostics.forElement(valueArgumentList).any { it.severity == Severity.ERROR }) return null + return valueArgumentList +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/.inspection b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/.inspection new file mode 100644 index 00000000000..877e0f3baf9 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.RedundantEnumConstructorInvocationInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt new file mode 100644 index 00000000000..75dbc850f28 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt @@ -0,0 +1,5 @@ +enum class Foo { + A(), + B(), + C +} diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt.after b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt.after new file mode 100644 index 00000000000..5e862ea010f --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt.after @@ -0,0 +1,5 @@ +enum class Foo { + A, + B, + C +} diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt new file mode 100644 index 00000000000..0997a65037e --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt @@ -0,0 +1,5 @@ +enum class Baz(i: Int = 0) { + A(1), + B(), + C(), +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt.after b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt.after new file mode 100644 index 00000000000..751056bfb63 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt.after @@ -0,0 +1,5 @@ +enum class Baz(i: Int = 0) { + A(1), + B, + C, +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasArgument.kt b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasArgument.kt new file mode 100644 index 00000000000..0fbbc141f15 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasArgument.kt @@ -0,0 +1,5 @@ +// PROBLEM: none +enum class Bar(i: Int) { + A(1), + B(2) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasError.kt b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasError.kt new file mode 100644 index 00000000000..4655b13f813 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasError.kt @@ -0,0 +1,6 @@ +// PROBLEM: none +// DISABLE-ERRORS +enum class Bar(i: Int) { + A(1), + B() +} \ 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 601e8560c09..86a64622ac1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -4763,6 +4763,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RedundantEnumConstructorInvocation extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInRedundantEnumConstructorInvocation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("basic.kt") + public void testBasic() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic.kt"); + } + + @TestMetadata("basic2.kt") + public void testBasic2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/basic2.kt"); + } + + @TestMetadata("hasArgument.kt") + public void testHasArgument() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasArgument.kt"); + } + + @TestMetadata("hasError.kt") + public void testHasError() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation/hasError.kt"); + } + } + @TestMetadata("idea/testData/inspectionsLocal/redundantExplicitType") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)