Skip to content

Commit

Permalink
SONARKT-286 Fix S6619 false positives caused by resolving binary expr…
Browse files Browse the repository at this point in the history
…ession operations too far (#393)
  • Loading branch information
johann-beleites-sonarsource authored Nov 27, 2023
1 parent f03be39 commit 47b8384
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ private fun <E : Throwable> moo(exception: E) =
cachedCtor(exception) as E?
}

private class FooBar(
something: Any
) {
private val someString: String? = something as? String
val isString: Boolean
get() = someString != null // Compliant
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ class UselessNullCheckCheck : AbstractCheck() {
}

private fun KtBinaryExpression.operandComparedToNull(bc: BindingContext): KtExpression? {
val left = left?.predictRuntimeValueExpression(bc) ?: return null
val right = right?.predictRuntimeValueExpression(bc) ?: return null
val leftResolved = left?.predictRuntimeValueExpression(bc) ?: return null
val rightResolved = right?.predictRuntimeValueExpression(bc) ?: return null

return when {
left.isNull() -> right
right.isNull() -> left
leftResolved.isNull() -> right
rightResolved.isNull() -> left
else -> null
}
}
Expand Down

0 comments on commit 47b8384

Please sign in to comment.