From 95448453150814d5236c5f09f197a13d687761c5 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Thu, 11 Mar 2021 17:42:00 +0300 Subject: [PATCH 1/5] Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block ### What's done: * Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda --- .../ruleset/rules/chapter3/BlockStructureBraces.kt | 8 +++++++- .../ruleset/rules/chapter3/files/BlankLinesRule.kt | 12 ++++++++++++ .../org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 6 ++++++ .../test/smoke/src/main/kotlin/Example8Expected.kt | 10 ++++++++++ .../test/smoke/src/main/kotlin/Example8Test.kt | 9 +++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt index 3cd13a9842..a8f7e16ed5 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt @@ -7,6 +7,7 @@ import org.cqfn.diktat.ruleset.constants.Warnings.BRACES_BLOCK_STRUCTURE_ERROR import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.* +import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BODY import com.pinterest.ktlint.core.ast.ElementType.CATCH @@ -232,7 +233,12 @@ class BlockStructureBraces(configRules: List) : DiktatRule( if (!configuration.closeBrace) { return } - val space = node.findChildByType(RBRACE)!!.treePrev + var space = node.findChildByType(RBRACE)!!.treePrev + node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { + if (space.text == "") { + space = space.treePrev + } + } if (checkBraceNode(space)) { BRACES_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "no newline before closing brace", (space.treeNext ?: node.findChildByType(RBRACE))!!.startOffset, node) { diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt index d055ffbf7f..0e59e97c63 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt @@ -3,6 +3,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.Warnings.TOO_MANY_BLANK_LINES import org.cqfn.diktat.ruleset.rules.DiktatRule +import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType import org.cqfn.diktat.ruleset.utils.getFirstChildWithType import org.cqfn.diktat.ruleset.utils.leaveExactlyNumNewLines import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine @@ -12,6 +13,7 @@ import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL +import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_ARGUMENT import com.pinterest.ktlint.core.ast.ElementType.LBRACE import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.SCRIPT @@ -44,6 +46,16 @@ class BlankLinesRule(configRules: List) : DiktatRule( it.elementType == BLOCK && it.treeParent?.elementType != SCRIPT || it.elementType == CLASS_BODY || it.elementType == FUNCTION_LITERAL }) { + node.findParentNodeWithSpecificType(LAMBDA_ARGUMENT)?.let { + if ((node.treeNext?.treeNext?.elementType == RBRACE) && (node.treePrev.elementType == LBRACE)) { + val freeText = "do not put newlines in empty lambda" + TOO_MANY_BLANK_LINES.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { + node.leaveExactlyNumNewLines(0) + } + return + } + } + if ((node.treeNext.elementType == RBRACE) xor (node.treePrev.elementType == LBRACE)) { // if both are present, this is not beginning or end // if both are null, then this block is empty and is handled in another rule diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 1bd5a84e29..ba2aa4d81e 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -94,6 +94,12 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt") } + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test #8`() { + fixAndCompare("Example8Expected.kt", "Example8Test.kt") + } + @Test @Tag("DiktatRuleSetProvider") fun `smoke test #7`() { diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt new file mode 100644 index 0000000000..0bf0f80eca --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt @@ -0,0 +1,10 @@ +package org.cqfn.diktat + +/* + * Copyright (c) Your Company Name Here. 2010-2020 + */ + +fun foo() { + run { } +} + diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt new file mode 100644 index 0000000000..795457988f --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt @@ -0,0 +1,9 @@ +/* + * Copyright (c) Your Company Name Here. 2010-2020 + */ + +fun foo() { + run { + + } +} From 7a64998cb1815cdeab20503f928f108fcfeb93bb Mon Sep 17 00:00:00 2001 From: Cheshiriks Date: Fri, 12 Mar 2021 15:22:36 +0300 Subject: [PATCH 2/5] Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block ### What's done: * Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda --- .../cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt | 2 +- .../cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt index a8f7e16ed5..c7bdf700c6 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt @@ -235,7 +235,7 @@ class BlockStructureBraces(configRules: List) : DiktatRule( } var space = node.findChildByType(RBRACE)!!.treePrev node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { - if (space.text == "") { + if (space.text.isEmpty()) { space = space.treePrev } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt index 0e59e97c63..5141ee3749 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt @@ -47,6 +47,7 @@ class BlankLinesRule(configRules: List) : DiktatRule( it.elementType == CLASS_BODY || it.elementType == FUNCTION_LITERAL }) { node.findParentNodeWithSpecificType(LAMBDA_ARGUMENT)?.let { + // Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE) if ((node.treeNext?.treeNext?.elementType == RBRACE) && (node.treePrev.elementType == LBRACE)) { val freeText = "do not put newlines in empty lambda" TOO_MANY_BLANK_LINES.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { From 8edfb4fa37ff7e7639ba94added139baa8dac42d Mon Sep 17 00:00:00 2001 From: Cheshiriks Date: Fri, 12 Mar 2021 18:15:31 +0300 Subject: [PATCH 3/5] Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block ### What's done: * Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda --- .../rules/chapter3/BlockStructureBraces.kt | 4 +-- .../ruleset/rules/chapter3/EmptyBlock.kt | 26 ++++++++++----- .../rules/chapter3/files/BlankLinesRule.kt | 6 +--- .../chapter3/BlockStructureBracesWarnTest.kt | 12 +++++++ .../ruleset/chapter3/EmptyBlockWarnTest.kt | 33 +++++++++---------- .../empty_block/EmptyBlockExpected.kt | 3 +- 6 files changed, 50 insertions(+), 34 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt index c7bdf700c6..a2b5b43144 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BlockStructureBraces.kt @@ -233,10 +233,10 @@ class BlockStructureBraces(configRules: List) : DiktatRule( if (!configuration.closeBrace) { return } - var space = node.findChildByType(RBRACE)!!.treePrev + val space = node.findChildByType(RBRACE)!!.treePrev node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { if (space.text.isEmpty()) { - space = space.treePrev + return } } if (checkBraceNode(space)) { 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 1034ed1063..202032a9d2 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 @@ -3,16 +3,12 @@ package org.cqfn.diktat.ruleset.rules.chapter3 import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig +import org.cqfn.diktat.ruleset.constants.Warnings import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_BLOCK_STRUCTURE_ERROR import org.cqfn.diktat.ruleset.rules.DiktatRule -import org.cqfn.diktat.ruleset.utils.findLBrace -import org.cqfn.diktat.ruleset.utils.findLeafWithSpecificType -import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType -import org.cqfn.diktat.ruleset.utils.hasParent -import org.cqfn.diktat.ruleset.utils.isBlockEmpty -import org.cqfn.diktat.ruleset.utils.isOverridden -import org.cqfn.diktat.ruleset.utils.isPascalCase +import org.cqfn.diktat.ruleset.utils.* +import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER @@ -41,12 +37,26 @@ class EmptyBlock(configRules: List) : DiktatRule( checkEmptyBlock(newNode, configuration) } - @Suppress("UnsafeCallOnNullableType") + private fun isEmptyWhiteSpace(node: ASTNode) = + node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: true + + @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkEmptyBlock(node: ASTNode, configuration: EmptyBlockStyleConfiguration) { if (node.treeParent.isOverridden() || isAnonymousSamClass(node)) { return } if (node.isBlockEmpty()) { + node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { + // Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE) + if (!configuration.emptyBlockExist && isEmptyWhiteSpace(node)) { + val freeText = "do not put newlines in empty lambda" + EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { + val whiteSpaceNode = node.findChildByType(WHITE_SPACE) + whiteSpaceNode?.leaveExactlyNumNewLines(0) + } + } + return + } if (!configuration.emptyBlockExist) { EMPTY_BLOCK_STRUCTURE_ERROR.warn(configRules, emitWarn, isFixMode, "empty blocks are forbidden unless it is function with override keyword", node.startOffset, node) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt index 5141ee3749..d638f25096 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt @@ -48,11 +48,7 @@ class BlankLinesRule(configRules: List) : DiktatRule( }) { node.findParentNodeWithSpecificType(LAMBDA_ARGUMENT)?.let { // Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE) - if ((node.treeNext?.treeNext?.elementType == RBRACE) && (node.treePrev.elementType == LBRACE)) { - val freeText = "do not put newlines in empty lambda" - TOO_MANY_BLANK_LINES.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { - node.leaveExactlyNumNewLines(0) - } + if (node.treeNext.text.isEmpty()) { return } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt index 6fbcb92a84..5d32c16ba9 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt @@ -85,6 +85,18 @@ class BlockStructureBracesWarnTest : LintTestBase(::BlockStructureBraces) { ) } + @Test + @Tag(WarningNames.BRACES_BLOCK_STRUCTURE_ERROR) + fun `check lambda with empty block`() { + lintMethod( + """ + |fun foo() { + | run { } + |} + """.trimMargin() + ) + } + @Test @Tag(WarningNames.BRACES_BLOCK_STRUCTURE_ERROR) fun `check empty block in else expression`() { 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 656ac7af05..0b835823d2 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 @@ -95,6 +95,20 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { ) } + @Test + fun `empty lambda`() { + lintMethod( + """ + |fun foo() { + | run { + | + | } + |} + """.trimMargin(), + LintError(2, 8, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} do not put newlines in empty lambda", true) + ) + } + @Test fun `check if-else expression without block`() { lintMethod( @@ -125,24 +139,9 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { lintMethod( """ |fun foo() { - | val y = listOf().map {} + | val y = listOf().map { } |} - """.trimMargin(), - LintError(2, 30, 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 empty lambda with config`() { - lintMethod( - """ - |fun foo() { - | val y = listOf().map {} - |} - """.trimMargin(), - LintError(2, 30, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} different style for empty block", true), - rulesConfigList = rulesConfigListEmptyBlockExist + """.trimMargin() ) } diff --git a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt index a47f33cf2b..8858adf874 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt @@ -10,8 +10,7 @@ fun foo () { fun goo () { var x = 10 if (x == 10) return else println(10) - val y = listOf().map { -} + val y = listOf().map {} for(x in 0..10) println(x) while (x > 0) --x From 18ae1abc254f4197a13b1f81d35bb95ed2d2b8de Mon Sep 17 00:00:00 2001 From: Cheshiriks Date: Fri, 12 Mar 2021 21:11:16 +0300 Subject: [PATCH 4/5] Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block ### What's done: * Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda --- .../ruleset/rules/chapter3/EmptyBlock.kt | 27 ++++++++++--------- .../chapter3/BlockStructureBracesWarnTest.kt | 4 ++- .../ruleset/chapter3/EmptyBlockWarnTest.kt | 25 +++++++++++++---- .../chapter3/files/BlankLinesWarnTest.kt | 14 ++++++++++ .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 6 ----- .../empty_block/EmptyBlockExpected.kt | 2 +- .../paragraph3/empty_block/EmptyBlockTest.kt | 4 ++- .../smoke/src/main/kotlin/Example8Expected.kt | 10 ------- .../smoke/src/main/kotlin/Example8Test.kt | 9 ------- 9 files changed, 55 insertions(+), 46 deletions(-) delete mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt delete mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt 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 202032a9d2..eba98545c6 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 @@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3 import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig -import org.cqfn.diktat.ruleset.constants.Warnings import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_BLOCK_STRUCTURE_ERROR import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.* @@ -37,7 +36,7 @@ class EmptyBlock(configRules: List) : DiktatRule( checkEmptyBlock(newNode, configuration) } - private fun isEmptyWhiteSpace(node: ASTNode) = + private fun isNewLine(node: ASTNode) = node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: true @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") @@ -46,21 +45,23 @@ class EmptyBlock(configRules: List) : DiktatRule( return } if (node.isBlockEmpty()) { - node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { - // Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE) - if (!configuration.emptyBlockExist && isEmptyWhiteSpace(node)) { - val freeText = "do not put newlines in empty lambda" - EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { - val whiteSpaceNode = node.findChildByType(WHITE_SPACE) - whiteSpaceNode?.leaveExactlyNumNewLines(0) - } - } - return - } if (!configuration.emptyBlockExist) { EMPTY_BLOCK_STRUCTURE_ERROR.warn(configRules, emitWarn, isFixMode, "empty blocks are forbidden unless it is function with override keyword", node.startOffset, node) } else { + node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let { + // Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE) + if (isNewLine(node)) { + val freeText = "do not put newlines in empty lambda" + EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { + val whiteSpaceNode = node.findChildByType(WHITE_SPACE) + whiteSpaceNode?.let { + node.replaceChild(whiteSpaceNode, PsiWhiteSpaceImpl(" ")) + } + } + } + return + } val space = node.findChildByType(RBRACE)!!.treePrev if (configuration.emptyBlockNewline && !space.text.contains("\n")) { EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block", diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt index 5d32c16ba9..b34828e1d4 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BlockStructureBracesWarnTest.kt @@ -91,7 +91,9 @@ class BlockStructureBracesWarnTest : LintTestBase(::BlockStructureBraces) { lintMethod( """ |fun foo() { - | run { } + | run { + | + | } |} """.trimMargin() ) 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 0b835823d2..66e82eb25e 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 @@ -100,12 +100,10 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { lintMethod( """ |fun foo() { - | run { - | - | } + | run { } |} """.trimMargin(), - LintError(2, 8, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} do not put newlines in empty lambda", true) + rulesConfigList = rulesConfigListEmptyBlockExist ) } @@ -134,6 +132,22 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { ) } + @Test + @Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR) + fun `check empty lambda with config`() { + lintMethod( + """ + |fun foo() { + | val y = listOf().map { + | + | } + |} + """.trimMargin(), + LintError(2, 30, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} do not put newlines in empty lambda", true), + rulesConfigList = rulesConfigListEmptyBlockExist + ) + } + @Test fun `check empty lambda`() { lintMethod( @@ -141,7 +155,8 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) { |fun foo() { | val y = listOf().map { } |} - """.trimMargin() + """.trimMargin(), + LintError(2, 30, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false) ) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt index 02a2b2947b..ff4ddae24e 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt @@ -30,6 +30,20 @@ class BlankLinesWarnTest : LintTestBase(::BlankLinesRule) { ) } + @Test + @Tag(WarningNames.TOO_MANY_BLANK_LINES) + fun `check lambda with empty block`() { + lintMethod( + """ + |fun foo() { + | run { + | + | } + |} + """.trimMargin() + ) + } + @Test @Tag(WarningNames.TOO_MANY_BLANK_LINES) fun `should prohibit usage of two or more consecutive blank lines`() { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index ba2aa4d81e..1bd5a84e29 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -94,12 +94,6 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt") } - @Test - @Tag("DiktatRuleSetProvider") - fun `smoke test #8`() { - fixAndCompare("Example8Expected.kt", "Example8Test.kt") - } - @Test @Tag("DiktatRuleSetProvider") fun `smoke test #7`() { diff --git a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt index 8858adf874..debdaa86fe 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockExpected.kt @@ -10,7 +10,7 @@ fun foo () { fun goo () { var x = 10 if (x == 10) return else println(10) - val y = listOf().map {} + val y = listOf().map { } for(x in 0..10) println(x) while (x > 0) --x diff --git a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockTest.kt b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockTest.kt index 7316e61550..fb5faa35ff 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/empty_block/EmptyBlockTest.kt @@ -9,7 +9,9 @@ fun foo () { fun goo () { var x = 10 if (x == 10) return else println(10) - val y = listOf().map {} + val y = listOf().map { + + } for(x in 0..10) println(x) while (x > 0) --x diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt deleted file mode 100644 index 0bf0f80eca..0000000000 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Expected.kt +++ /dev/null @@ -1,10 +0,0 @@ -package org.cqfn.diktat - -/* - * Copyright (c) Your Company Name Here. 2010-2020 - */ - -fun foo() { - run { } -} - diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt deleted file mode 100644 index 795457988f..0000000000 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example8Test.kt +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (c) Your Company Name Here. 2010-2020 - */ - -fun foo() { - run { - - } -} From c6f0954ca0c0a592ab425cbd9e805a95f79591a0 Mon Sep 17 00:00:00 2001 From: Cheshiriks Date: Mon, 15 Mar 2021 13:25:09 +0300 Subject: [PATCH 5/5] Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block ### What's done: * Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/EmptyBlock.kt | 2 +- 1 file changed, 1 insertion(+), 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 eba98545c6..f0405440c5 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 @@ -37,7 +37,7 @@ class EmptyBlock(configRules: List) : DiktatRule( } private fun isNewLine(node: ASTNode) = - node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: true + node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: false @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkEmptyBlock(node: ASTNode, configuration: EmptyBlockStyleConfiguration) {