Skip to content

Add wrong-t checker for incorrect testing.T usage in testify assertions#304

Open
mmorel-35 wants to merge 2 commits into
Antonboom:masterfrom
mmorel-35:issue-197
Open

Add wrong-t checker for incorrect testing.T usage in testify assertions#304
mmorel-35 wants to merge 2 commits into
Antonboom:masterfrom
mmorel-35:issue-197

Conversation

@mmorel-35

@mmorel-35 mmorel-35 commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Two related misuse patterns cause silent test failures or panics at runtime: passing nil/freshly-created *testing.T to package-level assertions, and using an *Assertions object bound to a parent t inside a t.Run subtest where t is shadowed by a fresh child *testing.T.

New checker: wrong-t (enabled by default, no autofix)

Case 1 — nil or fresh *testing.T as the t argument

// ❌ causes nil-deref panic or misattributed failures
assert.Equal(nil, expected, actual)

u := &testing.T{}
assert.Equal(u, expected, actual)   // variable form also caught

assert.Equal(&testing.T{}, expected, actual)
assert.NoError(new(testing.T), err)

// ✅
assert.Equal(t, expected, actual)

Detected by inspecting the first parameter type of the called function's signature (assert.TestingT / require.TestingT) and checking whether the supplied argument is nil, a composite literal of testing.T, a new(testing.T) call, or a variable declared from one of those expressions.

Case 2 — *Assertions object created with outer t used in a subtest

// ❌ 'a' is bound to the parent t; failures are reported against the wrong test
a := assert.New(t)
t.Run("subtest", func(t *testing.T) {
    a.Equal(expected, actual)
})

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

Detected by collecting all := assignments from assert.New/require.New, then for each t.Run callback (with a *testing.T parameter) walking its direct body — stopping at nested function literals to avoid double-reporting — and flagging any receiver whose declaration precedes the callback.

Files

  • internal/checkers/wrong_t.go — checker implementation (AdvancedChecker)
  • internal/checkers/checkers_registry.go — registration (enabled by default)
  • internal/checkers/checkers_registry_test.go, analyzer/checkers_factory_test.go — updated expected lists
  • internal/testgen/gen_wrong_t.go, internal/testgen/main.go — test-data generator
  • analyzer/testdata/src/checkers-default/wrong-t/ — generated test data
  • README.md — checker documentation

Closes #255
Closes #197

…ions

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Copilot AI added a commit to mmorel-35/testifylint that referenced this pull request May 28, 2026
mmorel-35 pushed a commit to mmorel-35/testifylint that referenced this pull request May 28, 2026
@Antonboom Antonboom added the llm-based LLM shit label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-based LLM shit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect use of Assertions object with wrong testing.T Use of testing.T not passed to the test function.

2 participants