You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a new custom Go analysis linter errortypeassertion that flags type assertions on the built-in error interface and recommends errors.As for proper error chain traversal.
What the linter catches
Any TypeAssertExpr where:
The expression being asserted (ta.X) has the built-in error type, and
The asserted-to type is a concrete type (not another interface)
Example flagged code:
// Both of these bypass error wrapping — flaggedifpathErr, ok:=err.(*os.PathError); ok { ... }
pathErr:=err.(*os.PathError)
Correct code that passes the linter:
// errors.As traverses the error chain correctlyvarpathErr*os.PathErroriferrors.As(err, &pathErr) { ... }
// Interface assertions are fine — checking optional behaviorste, ok:=err.(interface{ Timeout() bool })
Why it matters
Using err.(*T) — even the safe two-value form — bypasses error wrapping. When errors are wrapped with fmt.Errorf("...: %w", err), a direct type assertion fails silently while errors.As succeeds. This is a correctness bug, not just a style issue.
How it relates to existing linters
The existing uncheckedtypeassertion linter catches the panic-prone single-value form (v := x.(T)) across all types. This new linter:
Is specific to the error interface
Catches both the single-value and ok-checked two-value forms
Emits a different diagnostic pointing to errors.As instead of just the two-value form
Evidence
Discussion mining (14-day window): 5 issues found; none directly related but uncheckedtypeassertion usage confirmed awareness of type assertion problems in the codebase
Code pattern scanning: Existing code already uses errors.As correctly in many places; this linter prevents future regressions
pkg/linters/errortypeassertion/errortypeassertion_test.go — unit tests
pkg/linters/errortypeassertion/testdata/src/errortypeassertion/errortypeassertion.go — test fixtures
cmd/linters/main.go — registers the new analyzer
pkg/linters/README.md — documentation
Warning
Protected Files — Push Permission Denied
This was originally intended as a pull request, but the patch modifies protected files. A human must create the pull request manually.
Protected files
README.md
The push was rejected because GitHub Actions does not have workflows permission to push these changes, and is never allowed to make such changes, or other authorization being used does not have this permission.
Create the pull request manually
# Download the patch from the workflow run
gh run download 28392681205 -n agent -D /tmp/agent-28392681205
# Create a new branch
git checkout -b linter-miner/errortypeassertion-248c0e9249f7295c main
# Apply the patch (--3way handles cross-repo patches)
git am --3way /tmp/agent-28392681205/aw-linter-miner-errortypeassertion.patch
# Push the branch and create the pull request
git push origin linter-miner/errortypeassertion-248c0e9249f7295c
gh pr create --title '[linter-miner] feat(linters): add errortypeassertion linter' --base main --head linter-miner/errortypeassertion-248c0e9249f7295c --repo github/gh-aw
Summary
Add a new custom Go analysis linter
errortypeassertionthat flags type assertions on the built-inerrorinterface and recommendserrors.Asfor proper error chain traversal.What the linter catches
Any
TypeAssertExprwhere:ta.X) has the built-inerrortype, andExample flagged code:
Correct code that passes the linter:
Why it matters
Using
err.(*T)— even the safe two-value form — bypasses error wrapping. When errors are wrapped withfmt.Errorf("...: %w", err), a direct type assertion fails silently whileerrors.Assucceeds. This is a correctness bug, not just a style issue.How it relates to existing linters
The existing
uncheckedtypeassertionlinter catches the panic-prone single-value form (v := x.(T)) across all types. This new linter:errorinterfaceerrors.Asinstead of just the two-value formEvidence
uncheckedtypeassertionusage confirmed awareness of type assertion problems in the codebaseerrors.Ascorrectly in many places; this linter prevents future regressionsFiles changed
pkg/linters/errortypeassertion/errortypeassertion.go— analyzer implementationpkg/linters/errortypeassertion/errortypeassertion_test.go— unit testspkg/linters/errortypeassertion/testdata/src/errortypeassertion/errortypeassertion.go— test fixturescmd/linters/main.go— registers the new analyzerpkg/linters/README.md— documentationWarning
Protected Files — Push Permission Denied
This was originally intended as a pull request, but the patch modifies protected files. A human must create the pull request manually.
Protected files
README.mdThe push was rejected because GitHub Actions does not have
workflowspermission to push these changes, and is never allowed to make such changes, or other authorization being used does not have this permission.Create the pull request manually