Repository: analysis-dev/diktat (kotlin)

Issue:
Incorrect fix in AVOID_NULL_CHECKS rule
## Describe the bug
Consider the code, where `leftSide` is some nullable list.\
diktat is not able for now to detect, that here is the nested check, which shouldn't be fixed
```kotlin
fun foo() {
    if (leftSide?.any { it == null } == true) {
        return
    }
}
```
Fix mode **breaks** the code:

## Observed behavior
```kotlin
fun foo() {
    it ?: run {
        return
    }
}
```

Pull request description:
### What's done:
* Enhance the notion of complex conditions, which shouldn't be fixed
* Add test

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 8885de7311..6ee7e9ce64 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
@@ -226,7 +226,8 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
                     setOf(ElementType.EQEQ, ElementType.EQEQEQ, ElementType.EXCLEQ, ElementType.EXCLEQEQEQ)
                         .contains(condition.operationToken) &&
                     // no need to raise warning or fix null checks in complex expressions
-                    !condition.isComplexCondition()
+                    !condition.isComplexCondition() &&
+                    !condition.isInLambda()
 
     /**
      * checks if condition is a complex expression. For example:
@@ -237,6 +238,15 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
         return this.parent is KtBinaryExpression
     }
 
+    /**
+     * Expression could be used in lambda:
+     * if (a.any { it == null })
+     */
+    private fun KtBinaryExpression.isInLambda(): Boolean {
+        // KtBinaryExpression is in lambda if it has a parent that is a block expression
+        return this.parent is KtBlockExpression
+    }
+
     private fun warnAndFixOnNullCheck(
         condition: KtBinaryExpression,
         canBeAutoFixed: Boolean,


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/NullChecksRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/NullChecksRuleWarnTest.kt
index 68f33f0eff..c932b8cbce 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/NullChecksRuleWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/NullChecksRuleWarnTest.kt
@@ -189,4 +189,30 @@ class NullChecksRuleWarnTest : LintTestBase(::NullChecksRule) {
                     " use 'requireNotNull' instead of require(myVar != null)", true),
         )
     }
+
+    @Test
+    @Tag(WarningNames.AVOID_NULL_CHECKS)
+    fun `null check in lambda which is in if-statement is ok`() {
+        lintMethod(
+            """
+            |fun foo() {
+            |    if (leftSide?.any { it == null } == true) {
+            |        return
+            |    }
+            |}
+            """.trimMargin()
+        )
+    }
+
+    @Test
+    @Tag(WarningNames.AVOID_NULL_CHECKS)
+    fun `null check in lambda which is in require is ok`() {
+        lintMethod(
+            """
+            |fun foo() {
+            |    require(leftSide?.any { it == null })
+            |}
+            """.trimMargin()
+        )
+    }
 }