diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/VisitorWrappers.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/VisitorWrappers.kt
index c9f60d7e796..a94c56d3ec2 100644
--- a/compiler/psi/src/org/jetbrains/kotlin/psi/VisitorWrappers.kt
+++ b/compiler/psi/src/org/jetbrains/kotlin/psi/VisitorWrappers.kt
@@ -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)
+ }
+ }
\ No newline at end of file
diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml
index 77801fe6c81..f670c15af41 100644
--- a/idea/resources/META-INF/plugin-common.xml
+++ b/idea/resources/META-INF/plugin-common.xml
@@ -3379,6 +3379,16 @@
language="kotlin"
/>
+
+
+
diff --git a/idea/resources/inspectionDescriptions/RedundantEmptyInitializerBlock.html b/idea/resources/inspectionDescriptions/RedundantEmptyInitializerBlock.html
new file mode 100644
index 00000000000..86cdc3f3c93
--- /dev/null
+++ b/idea/resources/inspectionDescriptions/RedundantEmptyInitializerBlock.html
@@ -0,0 +1,6 @@
+
+
+This inspection reports redundant empty initializer block.
+
+
+
diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEmptyInitializerBlockInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEmptyInitializerBlockInspection.kt
new file mode 100644
index 00000000000..388e603a2e6
--- /dev/null
+++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantEmptyInitializerBlockInspection.kt
@@ -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()
+ }
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/.inspection b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/.inspection
new file mode 100644
index 00000000000..e9ffddb01bd
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/.inspection
@@ -0,0 +1 @@
+org.jetbrains.kotlin.idea.inspections.RedundantEmptyInitializerBlockInspection
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt
new file mode 100644
index 00000000000..3bb84187f61
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt
@@ -0,0 +1,5 @@
+class Test {
+ init {
+
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt.after b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt.after
new file mode 100644
index 00000000000..96d83e0c503
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/empty.kt.after
@@ -0,0 +1,2 @@
+class Test {
+}
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt
new file mode 100644
index 00000000000..1a1c1872277
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt
@@ -0,0 +1,9 @@
+class Test {
+ // init comment1
+ init {
+ // init comment2
+ }
+
+ fun foo() {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt.after b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt.after
new file mode 100644
index 00000000000..33ad9a35401
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/hasComment.kt.after
@@ -0,0 +1,5 @@
+class Test {
+
+ fun foo() {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/notEmpty.kt b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/notEmpty.kt
new file mode 100644
index 00000000000..4db80cfe39a
--- /dev/null
+++ b/idea/testData/inspectionsLocal/redundantEmptyInitializerBlock/notEmpty.kt
@@ -0,0 +1,9 @@
+// PROBLEM: none
+class Test {
+ init {
+ foo()
+ }
+
+ fun 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 d1c44fee5a7..ac03d185338 100644
--- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java
+++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java
@@ -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)