mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin-dfa] Fix boxing around constant expression (KTIJ-23542)
GitOrigin-RevId: d2ee3c8b29c3a0865ba85c55f2d730c07691d2f0
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1e0789eda4
commit
7ea7be6a8b
+7
-4
@@ -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
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -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>)
|
||||
}
|
||||
Reference in New Issue
Block a user