https://kotlinlang.org logo
m

Mark

08/11/2021, 12:58 PM
“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

CLOVIS

08/11/2021, 1:22 PM
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

Mark

08/11/2021, 1:29 PM
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

CLOVIS

08/11/2021, 1:31 PM
Why is that a false-positive? Converting it to
when
would give the exact same behavior since it's else-if
m

Mark

08/11/2021, 1:32 PM
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

CLOVIS

08/11/2021, 1:39 PM
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

ephemient

08/11/2021, 6:10 PM
m

Mark

08/12/2021, 2:30 AM
Thanks @ephemient I’ve added some code there that minimally reproduces the issue.
18 Views