An error returned by a call is stored in a variable and overwritten or dropped before anything reads it

tx0/go:unread-errorrecommendedwarningbug

What it finds

``go data, err := read(path) data, err = decode(data) if err != nil { ` The error of read is assigned to err and replaced by the next assignment before any check, so a failed read goes unnoticed and decode works on bad data. The compiler accepts this because err` is read later; only the first value is lost.

The rule follows each function through the shared IR, where every assignment makes a new value, and asks the dataflow analysis which values are never needed on any path. A value is reported when it is the result of a call, it is assigned to a variable whose name marks it as an error (err, or ending in err or Err), and nothing reads it before it is overwritten or the function returns. Functions with constructs the lowering does not understand are skipped, and so are variables that the function explicitly discards with _ = err.

From the rule's source, unread_error.rs.

Example from the rule's tests
package p func read(path string) ([]byte, error) { return nil, nil } func decode(data []byte) ([]byte, error) { return data, nil } func f(path string) ([]byte, error) {	data, err := read(path)tx0 reports: the error stored in err here is never read	data, err = decode(data)	if err != nil {		return nil, err	}	return data, nil} func g(path string) ([]byte, error) {	data, err := read(path)	if err != nil {		return nil, err	}	data, err = decode(data)	return data, err} func h(path string) []byte {	data, err := read(path)	_ = err	return data}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

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

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