Repository: analysis-dev/diktat (kotlin)

Issue:
Useless auto-fix in AVOID_NULL_CHECKS rule
## Describe the bug
```kotlin
val x = if (y != null) f(y) else null
```
is autofixed into
```kotlin
val x = y?.let {
    f(y)
}
    ?: run {
        null
    }
```
but elvis is redundant here, because `y?.let()` will return `null` itself, if `y == null`.


## Environment information
* diktat version: 0.5.0

Pull request:
### What's done:
  * Fixed bug
  * Added test


This pull request closes #840

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt
index 1d897a65f5..1db26b8083 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt
@@ -83,7 +83,7 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
         }
     }
 
-    @Suppress("UnsafeCallOnNullableType")
+    @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
     private fun fixNullInIfCondition(condition: ASTNode,
                                      binaryExpression: KtBinaryExpression,
                                      isEqualToNull: Boolean) {
@@ -91,20 +91,24 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
         val thenCodeLines = condition.extractLinesFromBlock(THEN)
         val elseCodeLines = condition.extractLinesFromBlock(ELSE)
         val text = if (isEqualToNull) {
-            if (elseCodeLines.isNullOrEmpty()) {
-                "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}"
-            } else {
-                """
-                    |$variableName?.let {
-                    |${elseCodeLines.joinToString(separator = "\n")}
-                    |}
-                    |?: run {
-                    |${thenCodeLines?.joinToString(separator = "\n")}
-                    |}
-                """.trimMargin()
+            when {
+                elseCodeLines.isNullOrEmpty() -> "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}"
+                thenCodeLines!!.singleOrNull() == "null" -> """
+                        |$variableName?.let {
+                        |${elseCodeLines.joinToString(separator = "\n")}
+                        |}
+                    """.trimMargin()
+                else -> """
+                        |$variableName?.let {
+                        |${elseCodeLines.joinToString(separator = "\n")}
+                        |}
+                        |?: run {
+                        |${thenCodeLines.joinToString(separator = "\n")}
+                        |}
+                    """.trimMargin()
             }
         } else {
-            if (elseCodeLines.isNullOrEmpty()) {
+            if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.singleOrNull() == "null")) {
                 "$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n}"
             } else {
                 """


Test patch:
diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt
index e994196c76..2a05252416 100644
--- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt
+++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt
@@ -21,6 +21,10 @@ print("qwe")
 }
 ?: run {
 print("asd")
+}
+
+    some?.let {
+print("qweqwe")
 }
 
     some?.let {
@@ -28,6 +32,18 @@ print("qqq")
 }
 ?: run {
 print("www")
+}
+
+    some?.let {
+print("ttt")
+}
+
+    some?.let {
+print("ttt")
+}
+?: run {
+null
+value
 }
 }
 
diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt
index 5e7d750744..5936fcb536 100644
--- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt
+++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt
@@ -22,10 +22,29 @@ fun test() {
         print("qwe")
     }
 
+    if (some == null) {
+        null
+    } else {
+        print("qweqwe")
+    }
+
     if (some != null) {
         print("qqq")
     } else {
         print("www")
     }
+
+    if (some != null) {
+        print("ttt")
+    } else {
+        null
+    }
+
+    if (some != null) {
+        print("ttt")
+    } else {
+        null
+        value
+    }
 }