Add "Redundant empty initializer block" inspection

^KT-5008 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-04-25 08:43:48 +03:00
committed by Natalia Selezneva
parent c155bf680f
commit b650c7ab00
11 changed files with 122 additions and 0 deletions
@@ -440,3 +440,12 @@ fun postfixExpressionVisitor(block: (KtPostfixExpression) -> Unit) =
block(expression)
}
}
fun classInitializerVisitor(block: (KtClassInitializer) -> Unit) =
object : KtVisitorVoid() {
override fun visitClassInitializer(expression: KtClassInitializer) {
super.visitClassInitializer(expression)
block(expression)
}
}
+10
View File
@@ -3379,6 +3379,16 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantEmptyInitializerBlockInspection"
displayName="Redundant empty initializer block"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="INFO"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,6 @@
<html>
<body>
This inspection reports redundant empty initializer block.
</pre>
</body>
</html>
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtClassInitializer
import org.jetbrains.kotlin.psi.classInitializerVisitor
class RedundantEmptyInitializerBlockInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = classInitializerVisitor(fun(initializer) {
val body = initializer.body as? KtBlockExpression ?: return
if (body.statements.isNotEmpty()) return
holder.registerProblem(
initializer,
"Redundant empty initializer block",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveInitializerBlockFix()
)
})
private class RemoveInitializerBlockFix : LocalQuickFix {
override fun getName() = "Remove initializer block"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
(descriptor.psiElement as? KtClassInitializer)?.delete()
}
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RedundantEmptyInitializerBlockInspection
@@ -0,0 +1,5 @@
class Test {
<caret>init {
}
}
@@ -0,0 +1,2 @@
class Test {
}
@@ -0,0 +1,9 @@
class Test {
// init comment1
<caret>init {
// init comment2
}
fun foo() {
}
}
@@ -0,0 +1,5 @@
class Test {
fun foo() {
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
class Test {
<caret>init {
foo()
}
fun foo() {
}
}
@@ -6056,6 +6056,34 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RedundantEmptyInitializerBlock extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRedundantEmptyInitializerBlock() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("empty.kt")
public void testEmpty() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt");
}
@TestMetadata("hasComment.kt")
public void testHasComment() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt");
}
@TestMetadata("notEmpty.kt")
public void testNotEmpty() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/notEmpty.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantEnumConstructorInvocation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)