Repository: analysis-dev/diktat (kotlin)

Issue:
Incorrect warning message for COMPLEX_BOOLEAN_EXPRESSION rule
## Describe the bug
The following boolean condition triggers a warning with incorrect text:

```
        return if ((node is TomlKeyValueSimple) || (node is TomlKeyValueSimple)) {
            decodeValue().toString().toLowerCase() != "null"
        } else {
            // all other technical nodes (like tables) should not contain null values
            // so we will mark them with a not null mark
            true
        }
```

## Expected behavior
```
 [COMPLEX_BOOLEAN_EXPRESSION] too complex boolean expression, that can be simplified: (node
 is TomlKeyValueSimple) 
```
## Observed behavior
```
 [COMPLEX_BOOLEAN_EXPRESSION] too complex boolean expression, that can be simplified: (node
 is TomlKeyValueSimple) || (node is TomlKeyValueSimple)
```
## Steps to Reproduce

## Environment information
* diktat version: `1.0.0-rc.2`

Pull request:
### 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

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 baf74c0fe3..1de54ef6d9 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 6fb7b78e40..ba0b06ee8b 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 c7d2357d87..b7f65478b1 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 0000000000..a9e3cada82
--- /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 0000000000..a8b49db057
--- /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
+    }
+}