An error is compared with == to a standard sentinel error that is usually wrapped
What it finds
err == os.ErrNotExist: the functions of os, io/fs, context, database/sql and net return their sentinel errors wrapped more often than not, as os.Open returns a *PathError that wraps ErrNotExist, and code between the call and the check may wrap them again with %w. == compares with the sentinel itself and misses every wrapped one; errors.Is follows the chain. Methods named Is, which errors.Is calls, compare with == by design and are left alone, and so is ctx.Err(), which returns the sentinels of context as they are.
The sentinel must be one of a list, resolved through the imports of the file. io.EOF and the other errors that are returned as they are by convention are not on it. The fix calls errors.Is, when the file imports errors under a name; it is unsafe because code may depend on telling a wrapped error from the sentinel.
From the rule's source, sentinel_error_equality.rs.
package p import ( "context" "database/sql" "errors" "io" "os") func f(ctx context.Context, err error) int { if err == os.ErrNotExist {tx0 reports: == misses a wrapped os.ErrNotExist return 1 } if sql.ErrNoRows != err {tx0 reports: != misses a wrapped sql.ErrNoRows return 2 } if err == context.Canceled || errors.Is(err, context.DeadlineExceeded) {tx0 reports: == misses a wrapped context.Canceled return 3 } if ctx.Err() == context.Canceled || err == io.EOF { return 4 } return 0} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:sentinel-error-equality" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:sentinel-error-equality] reason="..."tx0 explain tx0/go:sentinel-error-equality shows the same in the terminal. Found a false positive? Report it.