Add "Redundant enum constructor invocation" inspection
#KT-29321 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
542d3c3532
commit
e8a8bc89c9
@@ -3239,6 +3239,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantEnumConstructorInvocationInspection"
|
||||
displayName="Redundant enum constructor invocation"
|
||||
groupPath="Kotlin"
|
||||
groupName="Redundant constructs"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant enum entry constructor invocation:
|
||||
|
||||
<pre>
|
||||
enum class Foo {
|
||||
A(), // This '()' is redundant.
|
||||
B(),
|
||||
C
|
||||
}
|
||||
</pre>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+53
@@ -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<KtEnumEntry>()?.containingClass()?.body?.getChildrenOfType<KtEnumEntry>()?.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
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantEnumConstructorInvocationInspection
|
||||
@@ -0,0 +1,5 @@
|
||||
enum class Foo {
|
||||
A(),
|
||||
B()<caret>,
|
||||
C
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
enum class Foo {
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
enum class Baz(i: Int = 0) {
|
||||
A(1),
|
||||
B()<caret>,
|
||||
C(),
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
enum class Baz(i: Int = 0) {
|
||||
A(1),
|
||||
B,
|
||||
C,
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
enum class Bar(i: Int) {
|
||||
A(1)<caret>,
|
||||
B(2)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// PROBLEM: none
|
||||
// DISABLE-ERRORS
|
||||
enum class Bar(i: Int) {
|
||||
A(1),
|
||||
B()<caret>
|
||||
}
|
||||
+33
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user