Repository: analysis-dev/diktat (kotlin)

Issue:
False-positive `SMART_CAST_NEEDED` with mutable properties
## Describe the bug
If a property is mutable an is used in `when`, then smart cast is impossible - it leads to a compilation error:
```kotlin
fun SomeClass.foo() = when (mutableProperty) {
    is Foo -> (mutableProperty as Foo)  // smart cast is required 'because 'mutableProperty' is a property that has open or custom getter'
}
```

## Expected behavior
Diktat should not trigger on such cases

## Observed behavior
Explicit casting is removed, leading to a compilation error.

## Environment information
* diktat version: 1.0.1

Pull request description:
### What's done:
* Simplify rule for `when` conditions

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/SmartCastRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/SmartCastRule.kt
index a3c5e23060..9c3c793cbc 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/SmartCastRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/SmartCastRule.kt
@@ -6,9 +6,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule
 import org.cqfn.diktat.ruleset.utils.KotlinParser
 import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
 import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType
-import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
 import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
-import org.cqfn.diktat.ruleset.utils.hasChildOfType
 import org.cqfn.diktat.ruleset.utils.hasParent
 import org.cqfn.diktat.ruleset.utils.search.findAllVariablesWithUsages
 
@@ -24,10 +22,7 @@ import com.pinterest.ktlint.core.ast.ElementType.IS_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
 import com.pinterest.ktlint.core.ast.ElementType.THEN
-import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
 import com.pinterest.ktlint.core.ast.ElementType.WHEN
-import com.pinterest.ktlint.core.ast.ElementType.WHEN_CONDITION_IS_PATTERN
-import com.pinterest.ktlint.core.ast.ElementType.WHEN_ENTRY
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.lexer.KtTokens
 import org.jetbrains.kotlin.psi.KtBlockExpression
@@ -52,7 +47,8 @@ class SmartCastRule(configRules: List<RulesConfig>) : DiktatRule(
         }
 
         if (node.elementType == WHEN) {
-            handleWhenCondition(node)
+            // Rule is simplified after https://github.com/analysis-dev/diktat/issues/1168
+            return
         }
     }
 
@@ -217,30 +213,6 @@ class SmartCastRule(configRules: List<RulesConfig>) : DiktatRule(
         }
     }
 
-    @Suppress("UnsafeCallOnNullableType")
-    private fun handleWhenCondition(node: ASTNode) {
-        /*
-            Check if there is WHEN_CONDITION_IS_PATTERN. If so delete 'as' in it's block
-            or call expression if it doesn't have block
-         */
-
-        val identifier = node.getFirstChildWithType(REFERENCE_EXPRESSION)?.text
-
-        node.getAllChildrenWithType(WHEN_ENTRY).forEach { entry ->
-            if (entry.hasChildOfType(WHEN_CONDITION_IS_PATTERN) && identifier != null) {
-                val type = entry.getFirstChildWithType(WHEN_CONDITION_IS_PATTERN)!!
-                    .getFirstChildWithType(TYPE_REFERENCE)?.text
-
-                val callExpr = entry.findAllDescendantsWithSpecificType(BINARY_WITH_TYPE).firstOrNull()
-                val blocks = listOf(IsExpressions(identifier, type ?: ""))
-
-                callExpr?.let {
-                    handleThenBlock(callExpr, blocks)
-                }
-            }
-        }
-    }
-
     private fun KtNameReferenceExpression.getLocalDeclaration(): KtProperty? = parents
         .mapNotNull { it as? KtBlockExpression }
         .first()


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/SmartCastRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/SmartCastRuleWarnTest.kt
index 263d3538bd..2aad444628 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/SmartCastRuleWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter4/SmartCastRuleWarnTest.kt
@@ -7,6 +7,7 @@ import org.cqfn.diktat.util.LintTestBase
 
 import com.pinterest.ktlint.core.LintError
 import generated.WarningNames.SMART_CAST_NEEDED
+import org.junit.jupiter.api.Disabled
 import org.junit.jupiter.api.Tag
 import org.junit.jupiter.api.Test
 
@@ -191,6 +192,7 @@ class SmartCastRuleWarnTest : LintTestBase(::SmartCastRule) {
     }
 
     @Test
+    @Disabled("Rule is simplified after https://github.com/analysis-dev/diktat/issues/1168")
     @Tag(SMART_CAST_NEEDED)
     fun `smart cast in when bad`() {
         lintMethod(
@@ -228,6 +230,19 @@ class SmartCastRuleWarnTest : LintTestBase(::SmartCastRule) {
         )
     }
 
+    @Test
+    @Tag(SMART_CAST_NEEDED)
+    fun `smart cast in when good 2`() {
+        lintMethod(
+            """
+            |fun SomeClass.foo() = when (mutableProperty) {
+            |    is Foo -> (mutableProperty as Foo).fooFoo()  // smart cast is required 'because 'mutableProperty' is a property that has open or custom getter'
+            |    else -> println("ok")
+            |}
+            """.trimMargin()
+        )
+    }
+
     @Test
     @Tag(SMART_CAST_NEEDED)
     fun `if with multiple is good`() {