Wrong Errors Is
ID |
go.wrong_errors_is |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
reliability, suspicious |
Description
Reports a call to errors.Is whose arguments are in the wrong order —
errors.Is(ErrNotFound, err) instead of errors.Is(err, ErrNotFound).
Rationale
errors.Is(err, target) takes the live error value first and the sentinel target second; it
unwraps the value’s chain looking for the target. Swapping the arguments compares the bare
sentinel — which wraps nothing — against the live error, so the check silently never matches and
the error-handling branch becomes dead code. Because the mistake compiles cleanly and only
misbehaves on the error path, it is easy to miss in testing.
To stay free of false positives the rule fires only on the unambiguous swap shape: the first
argument is a bare sentinel identifier (Err/err followed by an upper-case letter) and the
second argument is the bare identifier err.
if errors.Is(ErrNotFound, err) { ... } // FLAW — arguments swapped
if errors.Is(err, ErrNotFound) { ... } // OK
Remediation
Swap the arguments so the error value comes first and the sentinel target second:
errors.Is(err, ErrNotFound).