[kotlin-dfa] Fix boxing around constant expression (KTIJ-23542)

GitOrigin-RevId: d2ee3c8b29c3a0865ba85c55f2d730c07691d2f0
This commit is contained in:
Tagir Valeev
2022-11-11 12:17:31 +00:00
committed by intellij-monorepo-bot
parent 1e0789eda4
commit 7ea7be6a8b
3 changed files with 19 additions and 4 deletions
@@ -61,6 +61,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
@@ -141,12 +142,14 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
private fun processConstant(expr: KtExpression?): Boolean {
expr ?: return false
val context = expr.analyze(BodyResolveMode.PARTIAL)
val constant = ConstantExpressionEvaluator.getConstant(expr, context) ?: return false
val kotlinType = expr.getKotlinType() ?: return false
val value = constant.getValue(kotlinType) ?: return false
val constant = ConstantExpressionEvaluator.getConstant(expr, context) as? TypedCompileTimeConstant<*> ?: return false
val declaredType = expr.getKotlinType() ?: return false
val value = constant.getValue(declaredType) ?: return false
if (value !is Int && value !is Long && value !is Float && value !is Double && value !is String) return false
val dfConstantType = DfTypes.constant(value, kotlinType.toDfType())
val actualType = constant.type
val dfConstantType = DfTypes.constant(value, actualType.toDfType())
addInstruction(PushValueInstruction(dfConstantType, KotlinExpressionAnchor(expr)))
addImplicitConversion(expr, actualType, declaredType)
return true
}
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.test.TestMetadata
@TestMetadata("testData/inspections/dfa")
class KtDataFlowInspectionTest : KotlinLightCodeInsightFixtureTestCase() {
fun testAlwaysZero() = doTest()
fun testAnyType() = doTest()
fun testArrays() = doTest()
fun testBoolean() = doTest()
fun testBooleanConst() = doTest()
@@ -0,0 +1,11 @@
// WITH_STDLIB
fun main() {
val a: Any = 0
println(<warning descr="Condition 'a is Int' is always true">a is Int</warning>)
}
fun main2(b: Boolean) {
var a: Any = 0
if (b) a = 1
println(<warning descr="Condition 'a is Int' is always true">a is Int</warning>)
}