Preserve comments before unwrapped else blocks - #992
Open
martinfrancois wants to merge 1 commit into
Open
Conversation
4 tasks
martinfrancois
force-pushed
the
repro/unwrap-else-comment-before-else
branch
from
August 15, 2026 20:01
9ae338e to
c3a9e27
Compare
martinfrancois
force-pushed
the
repro/unwrap-else-comment-before-else
branch
from
August 16, 2026 02:53
c3a9e27 to
2cb3713
Compare
martinfrancois
marked this pull request as ready for review
August 17, 2026 08:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Suggested review order: 4 of 52 (Score: 9)
Review first: openrewrite/rewrite-migrate-java#1192
What's changed?
Preserves comments stored before an
elsekeyword whenUnwrapElseAfterReturnremoves thatelse. The comment is attached to the first unwrapped statement together with its original leading whitespace. Two regressions cover both multiline and inline starts of the formerelseblock.What's your motivation?
Recipe:
org.openrewrite.staticanalysis.UnwrapElseAfterReturn.UnwrapElseAfterReturnremoves anelseafter areturnand moves the else body up one level. When the developer wrote a comment right before theelsekeyword, that comment disappears.Before
Actual after the recipe
Using current main before this change.
The output still compiles, but the comment
// Handle false conditionis gone.Expected after the recipe
The output MUST keep
// Handle false conditionimmediately before the unwrappedreturn 2;statement, as the second text block in the test expects.The mechanism, read from
UnwrapElseAfterReturn.java: theflattenhelper carries over comments from the else block's prefix (betweenelseand{) and from the first statement inside the block. Both of those positions are already covered by the existing testscommentsEverywhereandcommentsOnlyInBlocks, added with #654 and #655. Comments in the else part's own prefix, between the then block's closing brace and theelsekeyword, are never copied, andifStatement.withElsePart(null)discards them. This assumes the standard recipe run; the new test uses plainrewriteRunlike its neighbors.I found this while preparing #979, which fixes a related defect in this recipe. The defect here reproduces on current main on its own, independent of #979.
Affected code in real projects
elastic/elasticsearchIndexResolver.java: the then branch ends withreturn new InvalidMappedField(...), and the comment// type is okay, check aggregationsits between the closing brace and theelsekeyword. The recipe from main unwraps theelseand deletes that comment.spring-projects/spring-frameworkWhatWgUrlParser.java: this parser annotates each branch with the corresponding step quoted from the WHATWG URL spec. Theif (endsInNumber(asciiDomain))branch ends with a return, so the recipe from main unwraps theelseand drops the// Return asciiDomain.spec-step comment that documents it.Anything in particular you'd like reviewers to focus on?
Please review the ordering of comments from the
elsepart, theelseblock, and its first statement. Also review the conditional whitespace transfer: whitespace from theelsepart is used only when that part owns comments, so comments already inside the block retain their existing line placement.Any additional context
Pre-existing tests changed: None.
Related PR #979 touches the same recipe but does not fix this comment position. The earlier #654 and #655 handle comments inside the else block; this change handles comments stored before the
elsekeyword../gradlew test --tests org.openrewrite.staticanalysis.UnwrapElseAfterReturnTestpasses all 22 tests./usr/bin/python3on this machine has nopip.This change was prepared with AI assistance. I reviewed the implementation, regression tests, generated output, and description.
Checklist
./gradlew buildlocally, and committed any resulting changes torecipes.csv