Skip to content

Commit

Permalink
EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block
Browse files Browse the repository at this point in the history
### What's done:
* added check for WHEN element

Closes #1539
  • Loading branch information
Cheshiriks committed Jan 31, 2023
1 parent 1f3f7b7 commit 7b8ea94
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(

private fun searchNode(node: ASTNode, configuration: EmptyBlockStyleConfiguration) {
val newNode = node.findLBrace()?.treeParent ?: return
if (!isAllowedEmptyBlock(newNode) && newNode.isBlockEmpty()) {
if (!isAllowedEmptyBlock(newNode) && ((newNode.elementType == ElementType.WHEN && newNode.isWhenBlockEmpty()) || newNode.isBlockEmpty())) {
checkEmptyBlock(newNode, configuration)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ fun ASTNode?.isBlockEmpty() = this?.let {
this.text.replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
} ?: true

/**
* check if WHEN element node text is empty (contains only left and right braces)
*/
fun ASTNode?.isWhenBlockEmpty() = this?.let {
val firstIndex = this.text.indexOf("{")
this.text.substring(firstIndex - 1).replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
} ?: true

/**
* Method that is trying to find and return child of this node, which
* 1) stands before the node with type @beforeThisNodeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ 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()
)
}

@Test
@Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
fun `check if expression with empty else block with config`() {
Expand Down

0 comments on commit 7b8ea94

Please sign in to comment.