Hello. I encountered a case where "usedAsExpression" property for "when" expression returns false, while I'm expecting it should return true.
Copy code
fun test(c: Boolean, d: Boolean) {
val a = if (c) {
val b = "One"
if (d) {
b
} else {
"Two"
}
} else {
"Three"
}
}
Here, "usedAsExpression" for the outer "if" is true, while for the inner "if" is false. Is that correct? Obviously, the inner "if" is used as expression as well as the outer one.
How can I check that condition beside "usedAsExpression" property?
d
dmitriy.novozhilov
06/12/2024, 1:34 PM
FirWhenExpression.usedAsExpression
is initialized only based on source structure without any resolution and it might be not precise
dmitriy.novozhilov
06/12/2024, 1:34 PM
So you can expect false-negatives from it (but not false-positives)
r
Roman
06/12/2024, 1:36 PM
So, the only option here is to do analysis of this expression on my side? K1 API worked in this case...
d
dmitriy.novozhilov
06/13/2024, 8:02 AM
Yes, it is the only option
dmitriy.novozhilov
06/13/2024, 8:03 AM
Note that
usedAsExpression
is an internal implementation detail without any contracts for outer consumers