Use Bytes Buffer String
ID |
go.use_bytes_buffer_string |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
CWE:1164, code-smell, readability |
Rationale
Both bytes.Buffer and strings.Builder expose a String() method that returns their contents
directly, so converting the result of Bytes() to a string allocates a second copy of the data for
no reason. The rule fires only when the argument is exactly a no-argument .Bytes() call whose
receiver resolves to one of those types; an unresolved receiver is left alone.
import "bytes"
func fn(buf bytes.Buffer) {
_ = string(buf.Bytes()) // FLAW — use buf.String()
_ = buf.String() // OK
}