From 08b65f8af70c539ca2e2850149e713868a839da1 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 8 Apr 2021 11:59:39 +0300 Subject: [PATCH] Fix codestyle warning && and more tests ### What's done: * Fix codestyle warning && and more tests --- .../chapter3/CollapseIfStatementsRule.kt | 2 +- .../CollapseIfStatementsRuleWarnTest.kt | 44 +++++++++++++++++++ .../CollapseIfStatementsExpected.kt | 22 ++++++++++ .../collapse_if/CollapseIfStatementsTest.kt | 25 +++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/CollapseIfStatementsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/CollapseIfStatementsRule.kt index d1c72c6303..0b54ebbb9c 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/CollapseIfStatementsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/CollapseIfStatementsRule.kt @@ -108,7 +108,7 @@ class CollapseIfStatementsRule(configRules: List) : DiktatRule( it.elementType == EOL_COMMENT || it.elementType == BLOCK_COMMENT } - ?.toList() ?: listOf() + ?.toList() ?: emptyList() } private fun collapse(parentNode: ASTNode, nestedNode: ASTNode) { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/CollapseIfStatementsRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/CollapseIfStatementsRuleWarnTest.kt index 19e66f00fd..94f7a76db5 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/CollapseIfStatementsRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/CollapseIfStatementsRuleWarnTest.kt @@ -158,6 +158,50 @@ class CollapseIfStatementsRuleWarnTest : LintTestBase(::CollapseIfStatementsRule ) } + @Test + @Tag(WarningNames.COLLAPSE_IF_STATEMENTS) + fun `comments in compound cond`() { + lintMethod( + """ + |fun foo() { + | // comment + | if (cond1) { + | /* + | Some comments + | */ + | // More comments + | if (cond2 || cond3) { + | doSomething() + | } + | } + |} + """.trimMargin(), + LintError(8, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true) + ) + } + + @Test + @Tag(WarningNames.COLLAPSE_IF_STATEMENTS) + fun `comments in multiple if-statements`() { + lintMethod( + """ + |fun foo() { + | if (cond1) { + | // comment + | if (cond2) { + | // comment 2 + | if (cond3) { + | doSomething() + | } + | } + | } + |} + """.trimMargin(), + LintError(4, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true), + LintError(6, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true) + ) + } + @Test @Tag(WarningNames.COLLAPSE_IF_STATEMENTS) fun `not nested if`() { diff --git a/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsExpected.kt index cc7d3531fb..3005bd8742 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsExpected.kt @@ -191,3 +191,25 @@ true) { doSomething() } } + +fun foo() { + // comment + if (cond1 && +/* + Some comments + */ +// More comments +(cond2 || cond3)) { + doSomething() + } +} + +fun foo() { + if (cond1 && +// comment +cond2 && +// comment 2 +cond3) { + doSomething() + } +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsTest.kt b/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsTest.kt index 9605b1b2c0..0bf7f773cc 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/collapse_if/CollapseIfStatementsTest.kt @@ -225,3 +225,28 @@ fun foo() { } } } + +fun foo() { + // comment + if (cond1) { + /* + Some comments + */ + // More comments + if (cond2 || cond3) { + doSomething() + } + } +} + +fun foo() { + if (cond1) { + // comment + if (cond2) { + // comment 2 + if (cond3) { + doSomething() + } + } + } +}