I'm experiencing some unreal `"Unreachable Code"` ...
# k2-adopters
g
I'm experiencing some unreal
"Unreachable Code"
with K2 Mode active. Has anyone got the same issue?
d
Providing some code examples would be helpfull
g
Eh! It's proprietary code, in my case, I don't know whether I can arrange a public showcase... :(
Copy code
const val DASH_PREFIXED_INPUT_EXPECTED_RESULT = "I am reachable for any input prefixed by `-`"
const val NULL_INPUT_EXPECTED_RESULT = "I am reachable for any null input"
val ANY_OTHER_INPUT_EXPECTED_RESULT = null

fun main() {
    check(unreachable("-hello") == DASH_PREFIXED_INPUT_EXPECTED_RESULT)
    check(unreachable(null) == NULL_INPUT_EXPECTED_RESULT)
    check(unreachable("goodbye") == null)
    println("All good")
}

fun unreachable(input: String?): String? {
    return input?.let {
        return if (it.startsWith("-")) {  
           DASH_PREFIXED_INPUT_EXPECTED_RESULT
        } else {
            ANY_OTHER_INPUT_EXPECTED_RESULT
        }
    } ?: NULL_INPUT_EXPECTED_RESULT
}
It should raise the alert
unreachable-k2-mode.png
d
Thanks. I believe it's a KTIJ-32807. You can workaround it by removing the
return
inside
let
, as it's effectively unused (at least in the example you've provided)
g
I believe the inner return is necessary, otherwise the Elvis would kick in The let expression would output a null, and the Elvis operator will return the fallback value Is that right?
d
Ah, you are right indeed.
r
JIC: This false positive “unreachable code” does not reproduce in K2 Mode anymore, at least in the current Nightly, so it should be fixed for you in 252 release Could you please share your IDEA version?
g
Sure