Skip to content

Commit 6ebfec2

Browse files
committed
gopls/internal/analysis: fix waitgroup modernizer formatting
Previously, the waitgroup modernizer directly printed the ast.Expr for the receiver in the call to sync.WaitGroup.Go(). This resulted in incorrect formatting when the sync.WaitGroup is inside a struct type. Use printer.Fprint for formatting instead. Fixes golang/go#74708 Change-Id: I32774163f309dcdd3d9f337898a6e0db3c9c0304 Reviewed-on: https://go-review.googlesource.com/c/tools/+/698155 Reviewed-by: Peter Weinberger <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5a268f4 commit 6ebfec2

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,35 @@ func _() {
150150
wg1.Done()
151151
}()
152152
}
153+
154+
type Server struct {
155+
wg sync.WaitGroup
156+
}
157+
158+
type ServerContainer struct {
159+
serv Server
160+
}
161+
162+
func _() {
163+
var s Server
164+
s.wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go"
165+
go func() {
166+
print()
167+
s.wg.Done()
168+
}()
169+
170+
var sc ServerContainer
171+
sc.serv.wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go"
172+
go func() {
173+
print()
174+
sc.serv.wg.Done()
175+
}()
176+
177+
var wg sync.WaitGroup
178+
arr := [1]*sync.WaitGroup{&wg}
179+
arr[0].Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go"
180+
go func() {
181+
print()
182+
arr[0].Done()
183+
}()
184+
}

gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go.golden

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,32 @@ func _() {
141141
wg1.Done()
142142
}()
143143
}
144+
145+
type Server struct {
146+
wg sync.WaitGroup
147+
}
148+
149+
type ServerContainer struct {
150+
serv Server
151+
}
152+
153+
func _() {
154+
var s Server
155+
// want "Goroutine creation can be simplified using WaitGroup.Go"
156+
s.wg.Go(func() {
157+
print()
158+
})
159+
160+
var sc ServerContainer
161+
// want "Goroutine creation can be simplified using WaitGroup.Go"
162+
sc.serv.wg.Go(func() {
163+
print()
164+
})
165+
166+
var wg sync.WaitGroup
167+
arr := [1]*sync.WaitGroup{&wg}
168+
// want "Goroutine creation can be simplified using WaitGroup.Go"
169+
arr[0].Go(func() {
170+
print()
171+
})
172+
}

gopls/internal/analysis/modernize/waitgroup.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package modernize
66

77
import (
8+
"bytes"
89
"fmt"
910
"go/ast"
11+
"go/printer"
1012
"slices"
1113

1214
"golang.org/x/tools/go/analysis"
@@ -108,6 +110,12 @@ func waitgroup(pass *analysis.Pass) {
108110
continue
109111
}
110112

113+
var addCallRecvText bytes.Buffer
114+
err := printer.Fprint(&addCallRecvText, pass.Fset, addCallRecv)
115+
if err != nil {
116+
continue // error getting text for the edit
117+
}
118+
111119
pass.Report(analysis.Diagnostic{
112120
Pos: addCall.Pos(),
113121
End: goStmt.End(),
@@ -127,7 +135,7 @@ func waitgroup(pass *analysis.Pass) {
127135
{
128136
Pos: goStmt.Pos(),
129137
End: goStmt.Call.Pos(),
130-
NewText: fmt.Appendf(nil, "%s.Go(", addCallRecv),
138+
NewText: fmt.Appendf(nil, "%s.Go(", addCallRecvText.String()),
131139
},
132140
// ... }()
133141
// -

0 commit comments

Comments
 (0)