Skip to content

Add redundant-assert checker for implied assert.Error assertions#310

Open
mmorel-35 wants to merge 3 commits into
Antonboom:masterfrom
mmorel-35:redundant-assert
Open

Add redundant-assert checker for implied assert.Error assertions#310
mmorel-35 wants to merge 3 commits into
Antonboom:masterfrom
mmorel-35:redundant-assert

Conversation

@mmorel-35

Copy link
Copy Markdown
Contributor

This adds a new checker to catch redundant assert.Error / assert.Errorf calls when a stronger error assertion on the same error expression already exists in the same block. The goal is to remove duplicate signal and keep error assertions minimal.

  • New checker: redundant-assert

    • Introduces an advanced checker that scans assertion calls per ast.BlockStmt.
    • Reports redundant assert.Error* when paired with a stronger assertion on the same error expression:
      • ErrorContains*
      • ErrorIs*
      • ErrorAs*
      • EqualError*
    • Uses expression-string matching (analysisutil.NodeString) for same-error detection.
    • Suggests an autofix that removes the redundant statement.
  • Checker integration

    • Registers redundant-assert in checker priority order and enables it by default.
    • Updates registry/factory expectations in checker/analyzer tests.
  • Docs and examples

    • Adds redundant-assert to the README checker table.
    • Adds a dedicated README section with invalid/valid patterns and autofix/default metadata.
  • Analyzer test coverage

    • Adds checker-specific testdata (.go + .golden) for:
      • assert.Error* + stronger assertion on same error (diagnostic expected)
      • assert object style (assObj.Error)
      • assert/require mixed stronger assertions
      • non-matching cases (different vars / different blocks / standalone checks)
// before
assert.Error(t, err)
assert.ErrorContains(t, err, "not found")

// after
assert.ErrorContains(t, err, "not found")

mmorel-35 added 2 commits May 29, 2026 11:18
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
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

Successfully merging this pull request may close these issues.

1 participant