-
Notifications
You must be signed in to change notification settings - Fork 2.3k
go/analysis/passes/loopclosure: recursively check last statements in statements like if, switch, and for #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…statements like if, switch, and for In golang/go#16520, Allan Donovan suggested the current loopclosure check could be extended to check more statements. The current loopclosure flags patterns like: for k, v := range seq { go/defer func() { ... k, v ... }() } The motivating example for this CL from golang/go#16520 was: 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. Allan suggested 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, Allan sent a sketch in CL 184537. This CL attempts to complete Allan'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#55972
This PR (HEAD: a220948) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Daniel Martí: Patch Set 1: Run-TryBot+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 1: Kokoro presubmit build starting for golang/tools/gopls-legacy/presubmit Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 1: gopls-CI+1 Kokoro presubmit build finished with status: SUCCESS Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 1: TryBot-Result+1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Robert Findley: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Tim King: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 2: Kokoro presubmit build starting for golang/tools/gopls-legacy/presubmit Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 2: gopls-CI+1 Kokoro presubmit build finished with status: SUCCESS Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Tim King: Patch Set 1: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
…h some adjustments to comments
…into vet-loopclosure
This PR (HEAD: 20cbf14) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
… loop for if statements
This PR (HEAD: 44200bf) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from thepudds: Patch Set 6: (7 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 6: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 6: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
…ast, and update its function comment
This PR (HEAD: 8478a24) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from Alan Donovan: Patch Set 9: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 9: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
This PR (HEAD: 4c66f99) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from Tim King: Patch Set 9: (5 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
This PR (HEAD: c62d9aa) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from thepudds: Patch Set 12: (8 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Alan Donovan: Patch Set 12: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
…ivial" statements; update doc string
This PR (HEAD: 1ba80ff) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from thepudds: Patch Set 14: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Alan Donovan: Patch Set 14: Code-Review+2 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Robert Findley: Patch Set 14: Code-Review+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Alan Donovan: Patch Set 14: Run-TryBot+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Daniel Martí: Patch Set 14: Run-TryBot+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 14: Kokoro presubmit build starting for golang/tools/gopls-legacy/presubmit Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Tim King: Patch Set 14: Code-Review+2 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from thepudds: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Robert Findley: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 14: gopls-CI-1 Kokoro presubmit build finished with status: FAILURE Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
This PR (HEAD: 04980e0) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/tools/+/452155 to see it. Tip: You can toggle comments from me using the |
Message from thepudds: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 14: TryBot-Result-1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Tim King: Patch Set 15: TryBot-Bypass+1 Code-Review+2 Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Tim King: Patch Set 15: Run-TryBot+1 -TryBot-Bypass Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Alan Donovan: Patch Set 15: Run-TryBot+1 Code-Review+2 (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from Gopher Robot: Patch Set 15: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
Message from kokoro: Patch Set 15: Kokoro presubmit build starting for golang/tools/gopls-legacy/presubmit Please don’t reply on this GitHub thread. Visit golang.org/cl/452155. |
…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#55972 Fixes golang/go#30649 Fixes golang/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]>
This PR is being closed because golang.org/cl/452155 has been merged. |
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 this CL, the motivating example from golang/go#16520 is:
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#55972
Fixes golang/go#30649
Fixes golang/go#32876