go.error_type_assertion
Correctness
Medium
Detects error type assertions using direct type assertion instead of errors.As().
Why It Matters
Section titled “Why It Matters”Direct type assertions on errors:
- Miss wrapped errors — Wrapped errors don’t match direct type checks
- Break error chains — Go 1.13+ error wrapping is ignored
- Lose error context — Miss errors wrapped with
fmt.Errorf("%w")
Example
Section titled “Example”// ❌ Before (misses wrapped errors)if e, ok := err.(*MyError); ok { handleMyError(e)}// ✅ After (handles wrapped errors)var myErr *MyErrorif errors.As(err, &myErr) { handleMyError(myErr)}What Unfault Detects
Section titled “What Unfault Detects”err.(*Type)type assertionsswitch err.(type)without unwrapping- Missing
errors.As()for custom error types
Auto-Fix
Section titled “Auto-Fix”Unfault replaces type assertions with errors.As():
var e *MyErrorif errors.As(err, &e) { // ...}