Skip to content

[linter-miner] feat(linters): add errortypeassertion linter #42319

Description

@github-actions

Summary

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:

  1. The expression being asserted (ta.X) has the built-in error type, and
  2. The asserted-to type is a concrete type (not another interface)

Example flagged code:

// Both of these bypass error wrapping — flagged
if pathErr, ok := err.(*os.PathError); ok { ... }
pathErr := err.(*os.PathError)

Correct code that passes the linter:

// errors.As traverses the error chain correctly
var pathErr *os.PathError
if errors.As(err, &pathErr) { ... }

// Interface assertions are fine — checking optional behaviors
te, 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

Files changed

  • pkg/linters/errortypeassertion/errortypeassertion.go — analyzer implementation
  • 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

Generated by Linter Miner · 176.5 AIC · ⌖ 11.9 AIC · ⊞ 6K ·

  • expires on Jul 6, 2026, 10:27 AM UTC-08:00

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions