Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: False-positive SMART_CAST_NEEDED with mutable properties #1196

Merged
merged 4 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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