You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
go/analysis/passes/loopclosure: recursively check last statements in statements like if, switch, and for
In golang/go#16520, there was a suggestion to extend the
current loopclosure check to check more statements.
The current loopclosure flags patterns like:
for k, v := range seq {
go/defer func() {
... k, v ...
}()
}
For this CL, the motivating example from golang/go#16520 is:
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
for j := 0; j < 1; j++ {
wg.Add(1)
go func() {
fmt.Printf("%d ", i)
wg.Done()
}()
}
}
wg.Wait()
The current loopclosure check does not flag this because of the
inner for loop, and the checker looks only at the last statement
in the outer loop body.
The suggestion is we redefine "last" recursively. For example,
if the last statement is an if, then we examine the last statements
in both of its branches. Or if the last statement is a nested loop,
then we examine the last statement of that loop's body, and so on.
A few years ago, Alan Donovan sent a sketch in CL 184537.
This CL attempts to complete Alan's sketch, as well as integrates
with the ensuing changes from golang/go#55972 to check
errgroup.Group.Go, which with this CL can now be recursively "last".
Updates golang/go#16520
Updates golang/go#55972Fixesgolang/go#30649Fixesgolang/go#32876
Change-Id: If66c6707025c20f32a2a781f6d11c4901f15742a
GitHub-Last-Rev: 04980e0
GitHub-Pull-Request: #415
Reviewed-on: https://go-review.googlesource.com/c/tools/+/452155
Reviewed-by: Tim King <[email protected]>
Run-TryBot: Tim King <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
gopls-CI: kokoro <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Run-TryBot: Alan Donovan <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
0 commit comments