Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block #1605

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
nulls marked this conversation as resolved.
Show resolved Hide resolved
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,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`() {
Expand Down