From bc9b0725e5dd35e86333f202e9fa18d2fd7b2f8c Mon Sep 17 00:00:00 2001
From: Monchi <poyo0315@gmail.com>
Date: Wed, 2 Oct 2019 18:41:59 +0900
Subject: [PATCH] Keep empty brace of companion object

---
 .../pinterest/ktlint/ruleset/standard/NoEmptyClassBodyRule.kt | 4 +++-
 .../src/test/resources/spec/no-empty-class-body/lint.kt.spec  | 4 ++++
 2 files changed, 7 insertions(+), 1 deletion(-)

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<HashMap<String, String>>() {}
 
 fun main() {}
 
+class C3 {
+    companion object {}
+}
+
 // expect
 // 1:10:Unnecessary block ("{}")
 // 2:28:Unnecessary block ("{}")