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

cmd/vet: check for improper use of t.Run and t.Parallel #18106

Closed
dsnet opened this issue Nov 29, 2016 · 6 comments
Closed

cmd/vet: check for improper use of t.Run and t.Parallel #18106

dsnet opened this issue Nov 29, 2016 · 6 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Nov 29, 2016

Consider the following test:

func Test(t *testing.T) {
	for _, name := range []string{"foo", "bar", "baz"} {
		data := name // This line is necessary
		t.Run(name, func(t *testing.T) {
			t.Parallel()
			t.Log(data)
		})
	}
}

Without the name := name line to capture the name variable, the subtest will execute with the wrong values as the testdata. Worst yet, the tests will run with the correct names, but the wrong data, completely misleading the user that the test is executing as expected.

The blog on subtests explicitly warns that this must be done:

Note that we need to capture the range variable to ensure that tc gets bound to the correct instance.

I propose that vet check for this mis-use. One possible algorithm:

  • If t.Parallel is called within a t.Run
  • and the body of t.Run following the t.Parallel references a variable scoped to the range of some outer loop (i.e., not captured)
  • then flag as potential issue.

\cc @dominikh

@quentinmit quentinmit added this to the Go1.9Maybe milestone Nov 29, 2016
@quentinmit quentinmit added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Nov 29, 2016
@quentinmit
Copy link
Contributor

What is the frequency with which this occurs?

@minux
Copy link
Member

minux commented Dec 7, 2016 via email

@gonzaloserrano
Copy link

For the record it just happened to us at Social Point. Maybe a rule in https://github.com/dominikh/go-staticcheck would make it too if in vet is not possible. cc/ @dominikh

@rsc
Copy link
Contributor

rsc commented Jun 12, 2017

#16520 is the generalization of this. This is marked as NeedsDecision but really it needs a concrete proposal of what gets checked and how, and an argument for why that will have approximately no false positives.

@rsc rsc added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 12, 2017
@rsc rsc modified the milestones: Go1.10, Go1.9Maybe Jun 12, 2017
@rsc rsc removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 12, 2017
@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 Jul 12, 2018
@findleyr
Copy link
Contributor

Just found this via search. For the record, this landed as #55972.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants