Repository: cqfn/diKTat
Pull request #1052 (2021-09-13 14:39:34)

NPE in COMPLEX_BOOLEAN_EXPRESSION rule
### What's done
* Modify warning message in aim to avoid confusions
* Fix NPE for conditions, which simplified to single statements in fix mode - they have no additional parenthesis

codecov[bot]: # [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) Report
> Merging [#1052](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (e469d96) into [master](https://codecov.io/gh/cqfn/diKTat/commit/56168af8940fff0fc68ffd1c163190fa7836ebf8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (56168af) will **decrease** coverage by `0.00%`.
> The diff coverage is `83.33%`.

[![Impacted file tree graph](https://codecov.io/gh/cqfn/diKTat/pull/1052/graphs/tree.svg?width=650&height=150&src=pr&token=P2quRDffpt&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)

```diff
@@             Coverage Diff              @@
##             master    #1052      +/-   ##
============================================
- Coverage     83.41%   83.40%   -0.01%     
- Complexity     2446     2447       +1     
============================================
  Files           102      102              
  Lines          6120     6124       +4     
  Branches       1832     1833       +1     
============================================
+ Hits           5105     5108       +3     
  Misses          273      273              
- Partials        742      743       +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `83.40% <83.33%> (-0.01%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) | Coverage Δ | |
|---|---|---|
| [...t/ruleset/rules/chapter3/BooleanExpressionsRule.kt](https://codecov.io/gh/cqfn/diKTat/pull/1052/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#diff-ZGlrdGF0LXJ1bGVzL3NyYy9tYWluL2tvdGxpbi9vcmcvY3Fmbi9kaWt0YXQvcnVsZXNldC9ydWxlcy9jaGFwdGVyMy9Cb29sZWFuRXhwcmVzc2lvbnNSdWxlLmt0) | `92.59% <83.33%> (-0.68%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Last update [56168af...e469d96](https://codecov.io/gh/cqfn/diKTat/pull/1052?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).


Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt
index baf74c0f..1de54ef6 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt
@@ -115,7 +115,7 @@ enum class Warnings(
     WRONG_NEWLINES(true, "3.6.2", "incorrect line breaking"),
     TRAILING_COMMA(true, "3.6.2", "use trailing comma"),
     COMPLEX_EXPRESSION(false, "3.6.3", "complex dot qualified expression should be replaced with variable"),
-    COMPLEX_BOOLEAN_EXPRESSION(true, "3.6.4", "too complex boolean expression, that can be simplified"),
+    COMPLEX_BOOLEAN_EXPRESSION(true, "3.6.4", "simplification could be produced for the too complex boolean expression"),
 
     // FixMe: autofixing will be added for this rule
     STRING_CONCATENATION(true, "3.15.1", "strings should not be concatenated using plus operator - use string templates instead if the statement fits one line"),
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt
index 6fb7b78e..ba0b06ee 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt
@@ -159,8 +159,13 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
             .toString()
             .replace("&", "&&")
             .replace("|", "||")
-            .drop(1)  // dropping first (
-            .dropLast(1)  // dropping last )
+
+        if (simplifiedExpr.toString().first() == '(' && simplifiedExpr.toString().last() == ')') {
+            correctKotlinBooleanExpression = correctKotlinBooleanExpression
+                .drop(1)
+                .dropLast(1)
+        }
+
         mapOfExpressionToChar.forEach { (key, value) ->
             correctKotlinBooleanExpression = correctKotlinBooleanExpression.replace(value.toString(), key)
         }


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BooleanExpressionsRuleFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BooleanExpressionsRuleFixTest.kt
index c7d2357d..b7f65478 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BooleanExpressionsRuleFixTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/BooleanExpressionsRuleFixTest.kt
@@ -19,4 +19,10 @@ class BooleanExpressionsRuleFixTest : FixTestBase("test/paragraph3/boolean_expre
     fun `check distributive law fixing`() {
         fixAndCompare("DistributiveLawExpected.kt", "DistributiveLawTest.kt")
     }
+
+    @Test
+    @Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
+    fun `check same expressions`() {
+        fixAndCompare("SameExpressionsInConditionExpected.kt", "SameExpressionsInConditionTest.kt")
+    }
 }
diff --git a/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionExpected.kt
new file mode 100644
index 00000000..a9e3cada
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionExpected.kt
@@ -0,0 +1,11 @@
+fun foo() {
+    if (a) {}
+    if (a) {}
+    if (a) {}
+
+    return if (node is TomlKeyValueSimple) {
+        decodeValue().toString().toLowerCase() != "null"
+    } else {
+        true
+    }
+}
diff --git a/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionTest.kt
new file mode 100644
index 00000000..a8b49db0
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph3/boolean_expressions/SameExpressionsInConditionTest.kt
@@ -0,0 +1,11 @@
+fun foo() {
+    if (a || a) {}
+    if (a && a) {}
+    if ((((a && a)))) {}
+
+    return if ((node is TomlKeyValueSimple) || (node is TomlKeyValueSimple)) {
+        decodeValue().toString().toLowerCase() != "null"
+    } else {
+        true
+    }
+}