Skip to content

Add require-len checker for fail-fast indexed access guards - #312

Open
mmorel-35 wants to merge 3 commits into
Antonboom:masterfrom
mmorel-35:require-len
Open

Add require-len checker for fail-fast indexed access guards#312
mmorel-35 wants to merge 3 commits into
Antonboom:masterfrom
mmorel-35:require-len

Conversation

@mmorel-35

Copy link
Copy Markdown
Contributor

Adds a dedicated require-len checker that enforces fail-fast length guards before indexed collection access. require-error is left untouched and remains focused solely on error assertions.

  • New require-len checker

    • Reports assert.Len* calls that guard indexed access but should use require instead
    • Reports assert.* calls that use indexed access without a prior require.Len / require.NotEmpty guard
    • Provides autofix that inserts require.Len(t, arr, N) before the assertion, using require.NotEmpty(t, arr) when only index 0 is accessed
    • The hasLenGuard check only accepts fail-fast (require.*) guards and verifies the asserted length is >= maxIndex+1
  • Missing require import handled in autofix

    • When the file does not yet import github.com/stretchr/testify/require, the fix also inserts the import
    • Uses require as the qualifier; falls back to require1, require2, etc. if require is already taken as a local identifier
  • Registered as enabled-by-default with dedicated README docs, analyzer fixtures, and golden autofix output

@mmorel-35
mmorel-35 force-pushed the require-len branch 3 times, most recently from 802c5f0 to fe51083 Compare June 1, 2026 12:40
@ccoVeille

Copy link
Copy Markdown
Contributor

While I like the feature it brings, I'm a bit annoyed by the complexity of the code.

I know this comment is not actionable, I'm sorry.

But I wanted to share my point of view

@Antonboom Antonboom added the llm-based LLM shit label Jun 22, 2026
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
…d deduplicated autofix

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
@mmorel-35
mmorel-35 force-pushed the require-len branch 2 times, most recently from 1450ce1 to 8a245a3 Compare July 10, 2026 10:36
Removed 'require-len' section from CONTRIBUTING.md.
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.

3 participants