Skip to content

Keep single-statement blocks that carry comments when converting if/else-if to switch - #1256

Merged
jkschneider merged 1 commit into
java25/security-manager-thread-groupfrom
java25/if-else-if-switch-comments
Oct 3, 2026
Merged

jkschneider merged 1 commit into
java25/security-manager-thread-groupfrom
java25/if-else-if-switch-comments

Conversation

@jkschneider

Copy link
Copy Markdown
Member

IfElseIfConstructToSwitch unwraps a { single statement } block into a switch arm expression and dropped any comment on the statement or on the block's braces. A block with a comment on the statement, before its closing brace, or on the block itself now stays a block.

Found by a team of coding agents reviewing a Moderne run of org.openrewrite.java.migrate.UpgradeToJava25 over eight open-source repositories (run 20261003103223-WtyA8). The issue is switch-loses-branch-comment in the run's io.moderne.RecipeIssues data table, and the fix was discussed and reviewed on the run's message board. A row from the table, as a generalized example:

Moving a cast declaration into an instanceof pattern drops the comment attached to the declaration. (zendesk/maxwell, src/main/java/com/zendesk/maxwell/row/MaxwellJson.java)

Before:

if (x instanceof Integer) { // special handling
    Integer i = (Integer) x;
    use(i);
}

What the recipe produced:

if (x instanceof Integer i) {
    use(i);
}

Expected:

if (x instanceof Integer i) { // special handling
    use(i);
}

Stacked on #1255 (7 of 9); merge that one first.

@jkschneider
jkschneider force-pushed the java25/if-else-if-switch-comments branch from 5077d86 to 611466b Compare October 3, 2026 15:51
@jkschneider
jkschneider merged commit eca8c3d into main Oct 3, 2026
1 check passed
@jkschneider
jkschneider deleted the java25/if-else-if-switch-comments branch October 3, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant