Repository: analysis-dev/diktat (kotlin)

Issue:
EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block
```kotlin
fun foo() {
    when (a) {
    }
}
```

EMPTY_BLOCK_STRUCTURE_ERROR should be raised on empty `when` block

Pull request description:
### What's done:
* added check for WHEN element

Closes #1539

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt
index b396daae2d..bfe06c4c3e 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt
@@ -246,7 +246,12 @@ fun ASTNode.hasParent(type: IElementType) = parent(type) != null
  * check text because some nodes have empty BLOCK element inside (lambda)
  */
 fun ASTNode?.isBlockEmpty() = this?.let {
-    this.text.replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
+    if (this.elementType == ElementType.WHEN) {
+        val firstIndex = this.text.indexOf("{")
+        this.text.substring(firstIndex - 1).replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
+    } else {
+        this.text.replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
+    }
 } ?: true
 
 /**


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/EmptyBlockWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/EmptyBlockWarnTest.kt
index 7fe2f4dbae..213ce928a8 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/EmptyBlockWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/EmptyBlockWarnTest.kt
@@ -39,6 +39,20 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) {
         )
     }
 
+    @Test
+    @Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
+    fun `check if WHEN element node text is empty`() {
+        lintMethod(
+            """
+                    |fun foo() {
+                    |    when (a) {
+                    |    }
+                    |}
+            """.trimMargin(),
+            LintError(2, 5, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false)
+        )
+    }
+
     @Test
     @Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
     fun `check if expression with empty else block with config`() {