Unreviewed changes
5 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: go/analysis/passes/modernize/stringsbuilder.go
Insertions: 1, Deletions: 2.
@@ -76,8 +76,7 @@
// Now check each candidate variable's decl and uses.
nextcand:
for _, v := range slices.SortedFunc(maps.Keys(candidates), lexicalOrder) {
- var edits []analysis.TextEdit
- var postEdits []analysis.TextEdit // these edits are emitted last
+ var edits, postEdits []analysis.TextEdit // postEdits are emitted last
// Check declaration of s has one of these forms:
//
```
Change information
Commit message:
go/analysis/passes/modernize: fix stringsbuilder self-referencing bug
Fixes a bug in the stringsbuilder modernizer where self-referencing assignments like "s += s" result in invalid code like "s.WriteString(s).String()".
The modernizer processes uses of the variable independently. For "s += expr", it generates an edit to replace " += " with ".WriteString(" and another to append ")" at the end of the statement. For rvalue uses of "s", it appends ".String()".
When "s" appears at the very end of "expr" (as the rightmost identifier), the ".String()" edit and the closing ")" edit target the same position. We need to emit the ".String()" edit before the closing ")" edit to avoid suggesting a bad fix.
Fixes golang/go#78465
Change-Id: I6e7e9e468de6e80cff52dc352ff436266c12acc3
Files:
- M go/analysis/passes/modernize/stringsbuilder.go
- M go/analysis/passes/modernize/testdata/src/stringsbuilder/stringsbuilder.go
- M go/analysis/passes/modernize/testdata/src/stringsbuilder/stringsbuilder.go.golden
Change size: M
Delta: 3 files changed, 112 insertions(+), 18 deletions(-)
Branch: refs/heads/master