Community
Participate
Working Groups
In a new test case I found this: X x = new X(); x = switch (i) { case 1 -> { x = null; break x; } default -> null; }; While all seems legal, the assignment "x = null" looks very suspicious to me. Will developers realize, that the switch *expression* produces a side effect which will be overwritten by the value of the switch expression itself? Change the case block to x = new X(); break null; and you have a perfect riddle. One reason this program violates quick intuition: the assignment inside the switch expression appears lexically below (perhaps much below) the final assignment of the switch expression's value, so it is very hard to see, that the outer assignment occurs *later*. I could imagine different levels of warning: - warn against any assignment of an outer local inside a switch expression. - only warn when switch expression's value is being assigned to the same variable - further filter to report only situations where the inner assigned value is never read The above example meets all these criteria. We may either pick one of these levels, or create one warning option with sub-options.