Skip to content

Commit

Permalink
Fix: False-positive SMART_CAST_NEEDED with mutable properties (#1196)
Browse files Browse the repository at this point in the history
### What's done:
* Simplify rule for `when` conditions
  • Loading branch information
kgevorkyan authored Jan 31, 2022
1 parent d140a2e commit 80a366e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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`() {
Expand Down

0 comments on commit 80a366e

Please sign in to comment.