The error of a standard library call that changes files, the environment or a value is dropped

tx0/go:ignored-errorrecommendedwarningbug

What it finds

os.MkdirAll(dir, 0o755) or json.Unmarshal(data, &v) as a statement: the call reports failure only through its error, which nothing reads, so the code goes on as if the directory existed or v held the data.

The function must be one of a list of functions of os that change the file system or the environment, Unmarshal of encoding/json or encoding/xml, or Decode and Encode called on the result of NewDecoder or NewEncoder of encoding/json, encoding/xml or encoding/gob, all resolved through the imports. Only expression statements are reported: _ = os.Chmod(path, mode) says that the error is dropped on purpose, and a deferred call is usually cleanup whose failure does not matter. For the same reason os.Remove and os.RemoveAll are not on the list.

From the rule's source, ignored_error.rs.

Example from the rule's tests
package p import (	"encoding/json"	"io"	"os") func f(dir string, data []byte, r io.Reader, w io.Writer) {	var v map[string]int	os.MkdirAll(dir, 0o755)tx0 reports: the error of os.MkdirAll is dropped	json.Unmarshal(data, &v)tx0 reports: the error of json.Unmarshal is dropped	json.NewDecoder(r).Decode(&v)tx0 reports: the error of json.NewDecoder(...).Decode is dropped	json.NewEncoder(w).Encode(v)tx0 reports: the error of json.NewEncoder(...).Encode is dropped	_ = os.Remove(dir)	defer os.RemoveAll(dir)	if err := os.Mkdir(dir, 0o755); err != nil {		return	}	os.Getenv("HOME")}
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:ignored-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:ignored-error] reason="..."

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