-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: rangeloop: inspect all go/defer statements prior to a loop back edge #30649
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
Comments
/cc @mvdan @alandonovan |
Making |
can the question be sovled? |
This is an interesting special case that we could easily extend the check to handle without the full generality of #16520, which is much harder to solve. Specifically: the go/defer statement is still the last statement within the loop body, if we use a slightly more generalized definition of "last" that traverses down into control constructs (such as |
Hi @luhexcp, FYI, I tried implementing an older idea from @adonovan and sent https://go.dev/cl/452155. With that CL, go vet now complains about your example that was not flagged before: func main() {
done := make(chan bool)
values := []string{"a", "b", "c"}
ok := true
for _, v := range values {
if ok {
go func() {
fmt.Println(v) // vet now warns loop variable v captured by func literal
done <- true
}()
}
}
for _ = range values {
<-done
}
} |
Change https://go.dev/cl/452155 mentions this issue: |
Running go vet on this file:
prints
If I change loop body to this:
then go vet prints nothing.
Looking at the vet source code, checkRangeLoop can just judge a go or defer statement for body to see whether a variable is capture, which means vet will be failed if it exists condition sentence before a go or defer statement
The text was updated successfully, but these errors were encountered: