fmt.Errorf prints an error with %v or %s, so callers cannot unwrap it
What it finds
fmt.Errorf("open config: %v", err): the new error carries only the text of err, so errors.Is and errors.As cannot reach the cause any more. %w prints the same text and keeps the cause reachable.
The format must be a constant string of the file without a %w verb, and the argument printed with %v or %s a variable whose name marks it as an error (err, or ending in err or Err). Hiding the cause can be deliberate, at the boundary of a package whose callers should not depend on it, so the rule is strict. When the format is a string literal in the call itself, the fix writes %w; it is unsafe, since callers may start to see the cause.
From the rule's source, error_not_wrapped.rs.
package p import "fmt" func f(name string, err error, parseErr error) error { if name == "" { return fmt.Errorf("open %s: %v", name, err)tx0 reports: err is printed with %v, so it cannot be unwrapped } if name == "x" { return fmt.Errorf("parse: %s", parseErr)tx0 reports: parseErr is printed with %s, so it cannot be unwrapped } if name == "y" { return fmt.Errorf("open %s: %w", name, err) } if name == "z" { return fmt.Errorf("%v and %w", parseErr, err) } return fmt.Errorf("open %q: %d", name, len(name))} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:error-not-wrapped" = "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:error-not-wrapped] reason="..."tx0 explain tx0/go:error-not-wrapped shows the same in the terminal. Found a false positive? Report it.