[tools] go/analysis/passes/modernize: fix stringsbuilder self-referencing bug

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Apr 14, 2026, 3:58:26 PM (19 hours ago) Apr 14
to Madeline Kalil, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Alan Donovan, golang-co...@googlegroups.com

Gopher Robot submitted the change with unreviewed changes

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
Auto-Submit: Madeline Kalil <mka...@google.com>
Reviewed-by: Alan Donovan <adon...@google.com>
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
Submit Requirements:
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I6e7e9e468de6e80cff52dc352ff436266c12acc3
Gerrit-Change-Number: 766680
Gerrit-PatchSet: 7
Gerrit-Owner: Madeline Kalil <mka...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages