“Cascade if can be replaced with when” warning but...
# announcements
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?
c
All warnings can be suppressed by clicking the right arrow on the warning list. Keyboard: ALT ENTER, select the warning, right arrow
Do you have an example of such a false positive? It might be useful to report it to the team
m
I only have an Android example. Maybe there’s a way to contrive something similar in pur Kotlin:
Copy code
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
    getVolumePathPre24(volumeId)
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
    getVolumePathApi24Pre30(volumeId)
} else {
    getVolumePathApi30(volumeId)
}
The called functions have the appropriate annotations like
@RequiresApi(Build.VERSION_CODES.N)
c
Why is that a false-positive? Converting it to
when
would give the exact same behavior since it's else-if
m
All warnings can be suppressed by clicking the right arrow on the warning list.
Ah, thanks, I was wondering why sometimes I was seeing the suppress options, and other times not.
Why is that a false-positive?
Because with
when
it won’t compile. The compiler doesn’t seem to figure out the API levels, unlike with
if
Call requires API level 24 (current min is 23): getVolumePathApi24Pre30
c
Ah, that looks like an Android bug? Not sure who this should be reported to
Still though the bug isn't a false positive on
when
, it's just that
when
is apparently broken. That code should be correct.
👍 2
e
m
Thanks @ephemient I’ve added some code there that minimally reproduces the issue.