An error variable is returned in the branch where it was just found to be nil

tx0/go:return-nil-errordefaultwarningbug

What it finds

if err == nil { return nil, err }: the branch runs only when err is nil, so it returns no error, and here no value either. The condition was almost always meant to be err != nil.

The condition must be err == nil or nil == err for a variable whose name marks it as an error (err, or ending in err or Err), and a return among the statements of the branch itself must give that variable as its last result and nil for every other result, with no statement of the branch assigning the variable before. A return err alone is a roundabout return nil that could be deliberate and is not reported.

From the rule's source, return_nil_error.rs.

Example from the rule's tests
package p func load(name string) (*int, error) { return nil, nil } func f(name string) (*int, error) {	v, err := load(name)	if err == nil {		return nil, errtx0 reports: err is nil in this branch, so nothing and no error are returned	}	if nil == err {		return v, err	}	if err == nil {		err = check(v)		return nil, err	}	if err != nil {		return nil, err	}	return v, nil} func g(name string) error {	_, err := load(name)	if err == nil {		return err	}	return nil} func check(*int) error { return nil }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/go:return-nil-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:return-nil-error] reason="..."

tx0 explain tx0/go:return-nil-error shows the same in the terminal. Found a false positive? Report it.