From 7b8ea94bd0ed6cd867a2f0e39566cfde696b706d Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Tue, 31 Jan 2023 14:12:14 +0300 Subject: [PATCH 1/3] EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block ### What's done: * added check for WHEN element Closes #1539 --- .../diktat/ruleset/rules/chapter3/EmptyBlock.kt | 2 +- .../org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 8 ++++++++ .../diktat/ruleset/chapter3/EmptyBlockWarnTest.kt | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt index 06a231f849..c7159b5f4e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt @@ -39,7 +39,7 @@ class EmptyBlock(configRules: List) : 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) } } 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..381f7e7387 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 @@ -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 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..73ac17d8dd 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,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`() { From dd8393d1d22f0b8e7fde3bc3135ab791083dc4df Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Tue, 31 Jan 2023 14:21:14 +0300 Subject: [PATCH 2/3] EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block ### What's done: * added check for WHEN element Closes #1539 --- .../org/cqfn/diktat/ruleset/chapter3/EmptyBlockWarnTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 73ac17d8dd..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 @@ -48,7 +48,8 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { | when (a) { | } |} - """.trimMargin() + """.trimMargin(), + LintError(2, 5, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false) ) } From 847f4e55eb56f293267b98579a4590f2dafcb710 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Tue, 31 Jan 2023 15:47:55 +0300 Subject: [PATCH 3/3] EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block ### What's done: * added check for WHEN element Closes #1539 --- .../diktat/ruleset/rules/chapter3/EmptyBlock.kt | 2 +- .../org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt index c7159b5f4e..06a231f849 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt @@ -39,7 +39,7 @@ class EmptyBlock(configRules: List) : DiktatRule( private fun searchNode(node: ASTNode, configuration: EmptyBlockStyleConfiguration) { val newNode = node.findLBrace()?.treeParent ?: return - if (!isAllowedEmptyBlock(newNode) && ((newNode.elementType == ElementType.WHEN && newNode.isWhenBlockEmpty()) || newNode.isBlockEmpty())) { + if (!isAllowedEmptyBlock(newNode) && newNode.isBlockEmpty()) { checkEmptyBlock(newNode, configuration) } } 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 381f7e7387..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,15 +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 -} ?: 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 + 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 /**