Preserve argument comments and ranged append behavior in string-builder conversion - #994
Draft
martinfrancois wants to merge 1 commit into
Draft
Conversation
…t and ranged append retainCommentInsideAppendArguments pins that a comment inside an append argument list survives flattening; today it is dropped. convertCharArrayRangeAppend pins flattening append(char[], int, int) to String.valueOf(char[], int, int); today the whole chain is left unchanged. Both are marked @ExpectedToFail until the recipe handles them.
4 tasks
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: 46 of 52 (Score: 1.5)
Review first: #993
What's changed?
Adds 2 known-failing tests to
ReplaceStringBuilderWithStringTest:retainCommentInsideAppendArgumentsreproduces a comment inside anappendargument list being dropped.convertCharArrayRangeAppendproposes convertingappend(char[], int, int)instead of skipping the whole chain.No recipe code changes. The 15 tests already in the class are unchanged, including
doNotChangeCharArrayRangeAppend, which pins the current behavior from #977. The new tests are marked@ExpectedToFailso the suite stays green; removing the mark shows each failure.What's your motivation?
Recipe:
org.openrewrite.staticanalysis.ReplaceStringBuilderWithString.Case 1: preserve an argument comment
Before
Actual after the recipe
Using current main, after the merge of #977.
Expected after the recipe
return "a" + String.valueOf(chars /* the array */);The chain itself flattens fine now, with the
String.valueOfwrap from #977. But the/* the array */comment is lost.flatMethodInvocationChaintakes the bare expression from the argument list and discards the padding that holds the trailing comment. After the recipe runs, the code should keep it:"a" + String.valueOf(chars /* the array */). Note this is a different spot than #511, which covered comments in the chain prefix before.append; the existingretainCommentstest already protects those.Case 2: convert a ranged
char[]appendBefore
new StringBuilder().append("a").append(chars, 0, 2).toString()Actual after the recipe
(unchanged)The recipe rejects every
appendcall with more than one argument. The measured red run reportsRecipe was expected to make a change but made no changes.Expected after the recipe
"a" + String.valueOf(chars, 0, 2)String.valueOf(char[], int, int)has the same semantics asappend(char[], int, int). I checked both forms with a small compiled program: both printedaxyforcharsof{x, y, z}and compared equal.Found while preparing #977, which fixed a related defect in this recipe.
Anything in particular you'd like reviewers to focus on?
The judgment call. The dropped comment is a genuine bug: user text is lost from otherwise correct output. The ranged append is only an improvement: the current no-change result is safe, and skipping it may be deliberate conservatism. If a fix lands for the second case, the sibling test
doNotChangeCharArrayRangeAppendmust be updated at the same time; the@ExpectedToFailreason string onconvertCharArrayRangeAppendsays so. If you agree these should change, I would gladly prepare the fix. If the ranged-append behavior is intended, feel free to close that part and I know it is settled.Any additional context
Pre-existing tests changed: None.
Related work on this recipe: #977 (merged, added the
String.valueOfwrapping forchar[]and thedoNotChangeCharArrayRangeAppendpin) and #511 (chain-prefix comments). Neither covers these two cases.This reproduction was prepared with AI assistance (Claude Code). I reviewed the tests and this description.
The added reproduction tests and the existing suite together cover changed and unchanged behavior. The known-failing tests remain disabled until implementation. The formatter run was calibrated per file; untouched lines were not reformatted.
Checklist
./gradlew buildlocally, and committed any resulting changes torecipes.csv