Skip to content
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

proposal: cmd/go: run vet loopclosure during go test #30176

Closed
neild opened this issue Feb 11, 2019 · 4 comments
Closed

proposal: cmd/go: run vet loopclosure during go test #30176

neild opened this issue Feb 11, 2019 · 4 comments
Labels
FrozenDueToAge Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@neild
Copy link
Contributor

neild commented Feb 11, 2019

Go test runs a high-confidence set of go vet checks when building a test binary. I propose adding the "loopclosure" check to that set.

The loopclosure check looks for references to loop variables from within nested functions. For example, it will catch the bug in the following code, which will usually (depending on the whim of the scheduler) print 222 rather than 012.:

for _, x := range []int{0, 1, 2} {
  go func() {
    fmt.Print(x)
  }()
}

We've been running this analyzer as part of all Go builds inside Google for a while now (not just tests). It catches a common coding mistake (FAQ entry https://golang.org/doc/faq#closures_and_goroutines). While it's theoretically possible that valid code could depend on the behavior it tests for, I've never seen any such code in practice. We should turn it on by default.

@gopherbot gopherbot added this to the Proposal milestone Feb 11, 2019
@martisch
Copy link
Contributor

Earlier open issue and discussion about adding a vet check #16520

@neild
Copy link
Contributor Author

neild commented Feb 12, 2019

Earlier open issue and discussion about adding a vet check #16520

Note that #16520 is about improving the vet check. This proposal is to add the already existing check to the default set run by go test.

@rsc
Copy link
Contributor

rsc commented Feb 13, 2019

Can someone characterize the false positives for loopclosure?
The usual rule for turning on a vet check during go test is that it
have basically no false positives, or the false positives are only
in code that you couldn't defend not rewriting anyway.

The docs in loopclosure.go say:

This analyzer checks for references to loop variables from within a
function literal inside the loop body. It checks only instances where
the function literal is called in a defer or go statement that is the
last statement in the loop body, as otherwise we would need whole
program analysis.

For example:

        for i, v := range s {
                go func() {
                        println(i, v) // not what you might expect
                }()
        }

See: https://golang.org/doc/go_faq.html#closures_and_goroutines

In this case it seems like the reason it's not on by default is it doesn't catch very much. It does catch this one example. Maybe that's enough?

Damien, you said that we've been running it inside Google and also claimed that it catches a common mistake, but it looks like it will flag very few of the actual mistakes I've seen, because of the restriction about looking only at the final statement of the loop. Do you know if this has caught real mistakes inside Google, and if so how many?

@rsc rsc changed the title proposal: add loopclosure to vet tests run by "go test" proposal: cmd/go: run vet loopclosure during go test Feb 13, 2019
@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 28, 2019
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants