Data is decoded into a value that is not a pointer, which cannot receive it

tx0/go:unmarshal-non-pointerdefaultwarningbug

What it finds

json.Unmarshal(data, cfg) with a struct cfg: decoding stores into what its target points to, so a target that is not a pointer cannot receive anything, and the call returns an InvalidUnmarshalError without decoding. A map or slice passed by value fails the same way. &cfg was meant.

Checked are Unmarshal of encoding/json and encoding/xml, and the method Decode called directly on NewDecoder(...) of those packages or of encoding/gob, resolved through the imports. The target must be a variable of this file whose declaration shows a type that is not a pointer: a struct, map, slice or array type, a predeclared type such as string, or a type of this file declared as one of those; or a value that is a composite literal or a make of a map or slice.

From the rule's source, unmarshal_non_pointer.rs.

Example from the rule's tests
package p import (	"encoding/json"	"io") type config struct{ Name string } type handle *config func f(data []byte, r io.Reader, h handle) error {	var c config	if err := json.Unmarshal(data, c); err != nil {tx0 reports: c is not a pointer		return err	}	m := map[string]any{}	if err := json.NewDecoder(r).Decode(m); err != nil {tx0 reports: m is not a pointer		return err	}	var v any	if err := json.Unmarshal(data, v); err != nil {		return err	}	if err := json.Unmarshal(data, h); err != nil {		return err	}	return json.Unmarshal(data, &c)}
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:unmarshal-non-pointer" = "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:unmarshal-non-pointer] reason="..."

tx0 explain tx0/go:unmarshal-non-pointer shows the same in the terminal. Found a false positive? Report it.