errors.As is given a target that is nil or not a pointer, and panics
What it finds
var e *PathError; errors.As(err, e): errors.As stores the error it finds through its second argument, which must be a non-nil pointer to a variable. Given nil, a nil pointer or a value that is not a pointer, it panics. The pointer to the variable, &e, was meant.
Reported are the literal nil, a composite literal without &, and a local variable declared as var e *T without a value that the function never assigns or takes the address of, so it is still nil. errors is resolved through the imports of the file.
From the rule's source, errors_as_target.rs.
package p import ( "errors" "os") type codeError struct{ code int } func (codeError) Error() string { return "" } func f(err error) bool { var pathErr *os.PathError if errors.As(err, pathErr) {tx0 reports: the target pathErr of errors.As is a nil pointer, so the call panics return true } if errors.As(err, codeError{}) {tx0 reports: the target codeError{} of errors.As is not a pointer, so the call panics return true } if errors.As(err, nil) {tx0 reports: the target nil of errors.As is nil, so the call panics return true } var linkErr *os.LinkError var target *codeError target = &codeError{} return errors.As(err, &linkErr) || errors.As(err, target)} 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:errors-as-target" = "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:errors-as-target] reason="..."tx0 explain tx0/go:errors-as-target shows the same in the terminal. Found a false positive? Report it.