FallThrough: don't add a break after a guarded early exit - #1008
Conversation
|
While looking at #229 I noticed this change also covers it — it's the same shape, an On case 0:
if (true) return i;
break; // added
default:
throw new IllegalStateException();which fails to compile with It doesn't do the rest of what #229 asks for, though. The case 0:
if (true) return i;
// fall through
default:This branch adds no comment, so that test still fails and stays Would you like the comment insertion as well, either here or as a follow-up? Happy to do it — I just didn't want to assume the recipe should start writing comments without checking first. |
|
@timtebeek this is the fix PR from #460. @knutwannheden the question above is really yours — for #229, would you still want the |
org.openrewrite.staticanalysis.CodeCleanup: JavadocToAsciidocTransformerConfigItemTest.escape:315 #460Per your call in the issue — the recipe should leave this shape alone.
What was happening
FallThroughtreated a case ending in anifwithout anelseas an unintentional fall-through and appended abreak, which changed behavior:breaks(Statement)returnedfalsefor anifwith noelse, no relief comment was present, so abreakwas appended.The change
An
ifwithout anelsewhose then part completes abruptly is a guarded early exit — the statements after it are reachable only when the guard is false, so a fall-through past it is deliberate rather than an omission.I added this as a separate
guardsWithEarlyExitcheck rather than relaxingbreaks(..), because the two mean different things.breaks(..)answers "does this always complete abruptly", and it is used recursively fortry,switchand blocks. Loosening it there regressednestedBlocks, wherecan still complete normally and so genuinely does need the
break.guardsWithEarlyExittherefore unwraps only blocks and labels, and deliberately does not descend intotryorswitch.The trade-off is that the same shape can be a real omission, so some detection is lost. That seemed to be the intent of "not making any change here makes most sense", but happy to narrow it further if you'd rather.
Tests
org.openrewrite.staticanalysis.CodeCleanup: JavadocToAsciidocTransformerConfigItemTest.escape:315 #460: the reduced quarkus case, the block-wrapped variant that reaches the check through the block recursion, and a negative case confirming abreakis still added when unguarded code follows the guard.Full suite locally on JDK 21: 2277 tests, 0 failures.