Pasting the following Groovy code in IntelliJ IDEA:

String x switch ("hi") { case "hi": x = "data" // Assignment is not used break default: throw new RuntimeException() } /*if ("hi" == "hi") { x = "data" } else { throw new RuntimeException() }*/ try { println x // Variable 'x' might not be assigned } finally { /*} catch (ignored) {*/ println x }

On the first println x inside the try statement, I get the inspection warning "Variable 'x' might not be assigned".

I'm confused in two ways:

if you replace the switch statement with a functionally equivalent if/else statement, the warning no longer shows

if you replace finally with catch (ignored) , the warning no longer shows

To me, this behavior makes no sense. Can somebody explain why the warning is showing in the first place and why replacing the things above fixes it?

I'm running IntelliJ v2018.1.2 (Ultimate) and Groovy plugin v9.0.