The content of a buffer is converted between string and bytes instead of read in the wanted form
What it finds
string(buf.Bytes()) or []byte(buf.String()) with a bytes.Buffer: converting what the buffer returns copies its content a second time, while the buffer already has the method that returns the wanted form.
The receiver must be a variable or parameter of this file declared as a bytes.Buffer or a pointer to one, or with a value made by new(bytes.Buffer), &bytes.Buffer{}, bytes.NewBuffer or bytes.NewBufferString. Reported are conversions with the predeclared string of its Bytes() and conversions to []byte of its String(). The fix calls the other method, which returns the same content, so it is safe.
From the rule's source, buffer_conversion.rs.
package p import ( "bytes" "math/big") func f(n *big.Int) (string, []byte, string) { var buf bytes.Buffer buf.WriteString("x") s := string(buf.Bytes())tx0 reports: string(buf.Bytes()) copies the content again; buf.String() returns it directly b := []byte(buf.String())tx0 reports: []byte(buf.String()) copies the content again; buf.Bytes() returns it directly return s, b, string(n.Bytes())} 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:buffer-conversion" = "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:buffer-conversion] reason="..."tx0 explain tx0/go:buffer-conversion shows the same in the terminal. Found a false positive? Report it.