Skip to content

Accept variable pinning #11

@qdm12

Description

@qdm12

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions