diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoEmptyClassBodyRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoEmptyClassBodyRule.kt index c1a22d8994..ef1c7b0f6a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoEmptyClassBodyRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoEmptyClassBodyRule.kt @@ -5,6 +5,7 @@ import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.LBRACE import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE +import com.pinterest.ktlint.core.ast.children import com.pinterest.ktlint.core.ast.isPartOf import com.pinterest.ktlint.core.ast.nextLeaf import org.jetbrains.kotlin.com.intellij.lang.ASTNode @@ -22,7 +23,8 @@ class NoEmptyClassBodyRule : Rule("no-empty-class-body") { n.elementType == LBRACE && n.nextLeaf { it.elementType != WHITE_SPACE }?.elementType == RBRACE } == true && - !node.isPartOf(KtObjectLiteralExpression::class) + !node.isPartOf(KtObjectLiteralExpression::class) && + node.treeParent.firstChildNode.children().none { it.text == "companion" } ) { emit(node.startOffset, "Unnecessary block (\"{}\")", true) if (autoCorrect) { diff --git a/ktlint-ruleset-standard/src/test/resources/spec/no-empty-class-body/lint.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/no-empty-class-body/lint.kt.spec index 2c24c2deda..7db23e4338 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/no-empty-class-body/lint.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/no-empty-class-body/lint.kt.spec @@ -18,6 +18,10 @@ val o = object : TypeReference>() {} fun main() {} +class C3 { + companion object {} +} + // expect // 1:10:Unnecessary block ("{}") // 2:28:Unnecessary block ("{}")