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

Accept variable pinning #11

Open
qdm12 opened this issue Apr 25, 2021 · 0 comments
Open

Accept variable pinning #11

qdm12 opened this issue Apr 25, 2021 · 0 comments

Comments

@qdm12
Copy link

qdm12 commented Apr 25, 2021

Hi there,

Great linter. Although I prefer to use it by pinning the variable inside the for loop, before calling t.Parallel() for test cases and the linter detects it as invalid. Maybe you could add a rule for such thing? There are a few cases I can think of:

Pinning the variable before t.Run

func Test(t *testing.T) {
	t.Parallel()
	testCases := map[string]struct {
		n int
	}{
		"zero": {n: 0},
		"one":  {n: 1},
	}
	for name, testCase := range testCases {
		testCase := testCase
		t.Run(name, func(t *testing.T) {
			t.Parallel()
			t.Log(testCase.n)
		})
	}
}

Pinning the variable inside t.Run before t.Parallel()

func Test(t *testing.T) {
	t.Parallel()
	testCases := map[string]struct {
		n int
	}{
		"zero": {n: 0},
		"one":  {n: 1},
	}
	for name, testCase := range testCases {
		t.Run(name, func(t *testing.T) {
			testCase := testCase
			t.Parallel()
			t.Log(testCase.n)
		})
	}
}

Pretty much pinning the variable between the for and the t.Parallel() should pass the linting, although some additional checks might be needed such as if the pinned variable has a different name etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant