A String or Error method formats its own receiver with fmt, which calls the method again

tx0/go:recursive-string-methoddefaultwarningbug

What it finds

func (s Status) String() string { return fmt.Sprintf("status %v", s) }: fmt prints a value that has a String method by calling that method, which formats the receiver again, and so on until the stack overflows. The same holds for Error. Converting the receiver to its underlying type first, as in int(s), breaks the cycle.

The method must be named String or Error, take no parameters and return a string, and call a printing or formatting function of fmt with its receiver itself as an argument that is printed with %v, %s or %q (not %#v, which calls GoString), or by Sprint or Sprintln. Both value and pointer receivers are checked.

From the rule's source, recursive_string_method.rs.

Example from the rule's tests
package p import "fmt" type status int func (s status) String() string {	return fmt.Sprintf("status %v", s)tx0 reports: formatting s here calls String again, without end} type code int func (c code) String() string {	return fmt.Sprintf("code %d", int(c))} type failure struct{ msg string } func (f *failure) Error() string {	return fmt.Sprint(f)tx0 reports: formatting f here calls Error again, without end} type node struct{ id int } func (n node) String() string { return fmt.Sprintf("%#v", n) } type plain struct{ n int } func (p plain) String() string { return fmt.Sprintf("%d", p.n) }
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:recursive-string-method" = "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:recursive-string-method] reason="..."

tx0 explain tx0/go:recursive-string-method shows the same in the terminal. Found a false positive? Report it.