Skip to content

Add redundant-error-assertion checker for implied assert.Error / require.Error assertions#310

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

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

Conversation

@mmorel-35

@mmorel-35 mmorel-35 commented May 29, 2026

Copy link
Copy Markdown
Contributor

This adds a new checker to catch redundant assert.Error* / require.Error* 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, regardless of whether the code uses assert or require.

  • New checker: redundant-error-assertion

    • Introduces an advanced checker that scans assertion calls per ast.BlockStmt.
    • Reports redundant assert.Error* / require.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, with reassignment safety checks preserved.
    • Suggests an autofix that removes the redundant statement.
  • Checker integration

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

    • Adds redundant-error-assertion 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 the same error
      • require.Error* + stronger assertion on the same error
      • mixed assert/require cases
      • assert object style (assObj.Error)
      • require object style (reqObj.Error)
      • non-matching cases such as different vars, different blocks, and standalone checks
// before
assert.Error(t, err)
require.ErrorContains(t, err, "not found")

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

@Antonboom Antonboom added the llm-based LLM shit label Jun 22, 2026
@mmorel-35 mmorel-35 force-pushed the redundant-assert branch 2 times, most recently from e607c86 to 97d8457 Compare July 10, 2026 10:14
@mmorel-35 mmorel-35 changed the title Add redundant-assert checker for implied assert.Error assertions Add redundant-error-assertion checker for implied assert.Error / require.Error assertions Jul 10, 2026
@mmorel-35 mmorel-35 force-pushed the redundant-assert branch 2 times, most recently from 1539e42 to 9c8ca45 Compare July 10, 2026 10:23
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
…date related tests

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

llm-based LLM shit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants