https://kotlinlang.org logo
b

Bernhard

05/06/2019, 10:53 AM
@karelpeeters
Copy code
val method = result.jsonObject?.get("result")?.asString
            ?: throwInsertionError(result)
        return when (method) {
c

Czar

05/06/2019, 11:28 AM
Hm... I'd rewrite it as
Copy code
private fun ResultType.jsonResultAsString() = jsonObject?.get("result")?.asString
           ?: throwInsertionError(result)
//...
return when (val method = result.jsonResultAsString()) {
Where
val method =
part is only needed if you're using
method
in branches' bodies
The inspection you mention has been useful to me on numerous occasions, so it is use-case dependent. Sometimes the refactoring I suggested above is not useful or possible, so, yeah, on those occasions the inspection is not welcome. But in general if I see that, it is usually and indication that relevant code can be polished further.
m

Mike

05/06/2019, 12:06 PM
and depending on what happens in the
when (method)
, IntelliJ may be suggesting that it all get rolled into one
when
statement…
a

Alexey Belkov [JB]

05/14/2019, 9:15 AM
This will be fixed in 1.3.40: https://youtrack.jetbrains.com/issue/KT-30457