“Cascade if can be replaced with when” warning but...
# android
m
“Cascade if can be replaced with when” warning but sometimes it’s a false positive, for example if I have an
if
condition (at least 3 branches) where the checks are things like
Build.VERSION.SDK_INT > 12
and then branch depends on such a later SDK level. How to suppress this warning? Thread in Slack Conversation
@kenkyee how is this “not kotlin”? It’s literally only relevant to Kotlin as a language, and Android as a platform.
k
It's a lint bug. Write the same thing in Java and you'll get the same issue...
m
Do you mean a
switch
statement?
k
Or an if else tree as well. Or if you use a method to do the check. It's just a buggy lint rule you end up suppressing half the time...
m
This is specifically about a
when
statement showing a compilation error when the equivalent
if
statement does not. How do you propose you write this in Java to get the same error?
Copy code
val capture = when {
    Build.VERSION.SDK_INT < Build.VERSION_CODES.R -> "fallback"
    else -> requires30() // compile-time error "Call requires API level 30 (current min is 23): requires30"
}

@RequiresApi(30)
fun requires30() {}
In other words, I think this is specifically to do with the (Lint perhaps) parsing of the
when
statement in Kotlin, rather than some language-independent thing.
k
Lint is actually part of AGP. File a bug at b.android.com with a reproducer
m
As linked in the original thread: https://issuetracker.google.com/issues/189459502 I still don’t understand how you think this is not Kotlin-specific.