Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ linters:
- pkg: golang.org/x/tools/go/analysis
desc: Please, implement helpers without usage of x/tools

exhaustive:
default-signifies-exhaustive: true

forbidigo:
forbid:
- pattern: panic
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ https://golangci-lint.run/docs/linters/configuration/#testifylint
| [suite-subtest-run](#suite-subtest-run) | ✅ | ❌ |
| [suite-thelper](#suite-thelper) | ❌ | ✅ |
| [useless-assert](#useless-assert) | ✅ | ❌ |
| [wrong-t](#wrong-t) | ✅ | ❌ |

> ⚠️ Also look at open for contribution [checkers](CONTRIBUTING.md#open-for-contribution)

Expand Down Expand Up @@ -1240,6 +1241,58 @@ assert.LessOrEqual(0, uintVal)

---

### wrong-t

```go
func TestFoo(t *testing.T) {
assert.Equal(nil, expected, actual)

u := &testing.T{}
assert.Equal(u, expected, actual)
}

func TestBar(t *testing.T) {
a := assert.New(t)

t.Run("subtest", func(t *testing.T) {
a.Equal(expected, actual) // a was created with the outer t
})
}

func TestFoo(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestBar(t *testing.T) {
t.Run("subtest", func(t *testing.T) {
a := assert.New(t)
a.Equal(expected, actual)
})
}
```

**Autofix**: false. <br>
**Enabled by default**: true. <br>
**Reason**: Protection from bugs and misleading test output.

The checker handles two related cases of incorrect `testing.T` usage:

#### 1) Nil or freshly created `testing.T` in package-level assertions

Passing `nil` or a freshly allocated `&testing.T{}` / `new(testing.T)` as the `t` argument will
cause a panic at runtime (nil dereference) or silently misattribute test failures.

#### 2) Assertion object created with outer `t` used in a subtest

When `assert.New(t)` or `require.New(t)` is called in an outer scope, the resulting
`*Assertions` object is bound to the outer `testing.T`. Using it inside a `t.Run` subtest
(which receives its own fresh `t`) causes test failures to be reported against the parent
test rather than the subtest, breaking the display of test names in failure output.

---

## Chain of warnings

Linter does not automatically handle the "evolution" of changes.
Expand Down
2 changes: 2 additions & 0 deletions analyzer/checkers_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewSuiteBrokenParallel(),
checkers.NewSuiteMethodSignature(),
checkers.NewSuiteSubtestRun(),
checkers.NewWrongT(),
}
allAdvancedCheckers := []checkers.AdvancedChecker{
checkers.NewBlankImport(),
Expand All @@ -71,6 +72,7 @@ func Test_newCheckers(t *testing.T) {
checkers.NewSuiteMethodSignature(),
checkers.NewSuiteSubtestRun(),
checkers.NewSuiteTHelper(),
checkers.NewWrongT(),
}

formatterWithoutEnabledOptions := checkers.RegularChecker(checkers.NewFormatter().
Expand Down
131 changes: 131 additions & 0 deletions analyzer/testdata/src/checkers-default/wrong-t/wrong_t_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/checkers/checkers_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var registry = checkersRegistry{
{factory: asCheckerFactory(NewSuiteMethodSignature), enabledByDefault: true},
{factory: asCheckerFactory(NewSuiteSubtestRun), enabledByDefault: true},
{factory: asCheckerFactory(NewSuiteTHelper), enabledByDefault: false},
{factory: asCheckerFactory(NewWrongT), enabledByDefault: true},
}

type checkersRegistry []checkerMeta
Expand Down
2 changes: 2 additions & 0 deletions internal/checkers/checkers_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestAll(t *testing.T) {
"suite-method-signature",
"suite-subtest-run",
"suite-thelper",
"wrong-t",
}
if !slices.Equal(expected, checkerList) {
t.Fatalf("unexpected list: %#v", checkerList)
Expand Down Expand Up @@ -98,6 +99,7 @@ func TestEnabledByDefault(t *testing.T) {
"suite-broken-parallel",
"suite-method-signature",
"suite-subtest-run",
"wrong-t",
}
if !slices.Equal(expected, checkerList) {
t.Fatalf("unexpected list: %#v", checkerList)
Expand Down
Loading