An error is compared with a newly made one, which never equals it
What it finds
err == errors.New("not found"): every call of errors.New or fmt.Errorf returns a new error, which no other error equals, so the comparison is always false, or always true with !=. The error to compare with is a variable made once, or errors.Is asks the question that was meant.
The callee must be New of the package errors or Errorf of fmt, resolved through the imports of the file.
From the rule's source, compare_new_error.rs.
package p import ( "errors" "fmt" e "errors") var errNotFound = errors.New("not found") func f(err error) bool { if err == errors.New("not found") {tx0 reports: this comparison is always false: errors.New makes a new error return true } if fmt.Errorf("x: %d", 1) != err {tx0 reports: this comparison is always true: fmt.Errorf makes a new error return true } if err == (e.New("x")) {tx0 reports: always false return true } if err == errNotFound || errors.Is(err, errNotFound) { return true } if err.Error() == errors.New("x").Error() { return true } { errors := fake{} return err == errors.New("x") }} type fake struct{} func (fake) New(string) error { return nil } Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/go:compare-new-error" = "off"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:compare-new-error] reason="..."tx0 explain tx0/go:compare-new-error shows the same in the terminal. Found a false positive? Report it.