KT-14329 Do not report inspection "Remove empty class body" for anonymous objects (#972)

This commit is contained in:
Yoshinori Isogai
2016-10-14 20:00:55 +09:00
committed by Dmitry Jemerov
parent 2b2042860d
commit 708a0e3b5d
9 changed files with 68 additions and 1 deletions
@@ -21,6 +21,8 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveEmptyClassBodyInspection : IntentionBasedInspection<KtClassBody>(RemoveEmptyClassBodyIntention::class), CleanupLocalInspectionTool {
override val problemHighlightType: ProblemHighlightType
@@ -30,5 +32,10 @@ class RemoveEmptyClassBodyInspection : IntentionBasedInspection<KtClassBody>(Rem
class RemoveEmptyClassBodyIntention : SelfTargetingOffsetIndependentIntention<KtClassBody>(KtClassBody::class.java, "Remove empty class body") {
override fun applyTo(element: KtClassBody, editor: Editor?) = element.delete()
override fun isApplicableTo(element: KtClassBody) = element.text.replace("{", "").replace("}", "").isBlank()
override fun isApplicableTo(element: KtClassBody): Boolean {
element.getStrictParentOfType<KtObjectDeclaration>()?.let {
if (it.isObjectLiteral()) return false
}
return element.text.replace("{", "").replace("}", "").isBlank()
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
interface Foo
fun test() {
val value = object : Foo {}<caret>
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun test() {
object {}<caret>
}
@@ -0,0 +1,3 @@
object Foo {
class Bar {}<caret>
}
@@ -0,0 +1,3 @@
object Foo {
class Bar
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
object Foo {
fun test() {
object {}<caret>
}
}
@@ -0,0 +1,4 @@
object Foo {<caret>
}
@@ -0,0 +1 @@
object Foo
@@ -10446,6 +10446,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyClassBody"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("anonymousInterfaceObject.kt")
public void testAnonymousInterfaceObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/anonymousInterfaceObject.kt");
doTest(fileName);
}
@TestMetadata("anonymousObject.kt")
public void testAnonymousObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/anonymousObject.kt");
doTest(fileName);
}
@TestMetadata("emptyClass.kt")
public void testEmptyClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/emptyClass.kt");
@@ -10464,11 +10476,29 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("innerClass.kt")
public void testInnerClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/innerClass.kt");
doTest(fileName);
}
@TestMetadata("nestedAnonymous.kt")
public void testNestedAnonymous() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/nestedAnonymous.kt");
doTest(fileName);
}
@TestMetadata("noneEmptyClass.kt")
public void testNoneEmptyClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/noneEmptyClass.kt");
doTest(fileName);
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyClassBody/object.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall")