Unnecessary fmt.Sprintf For A String
ID |
go.no_sprintf_for_string |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
Go |
Tags |
best-practice, code-style |
Description
Reports fmt.Sprintf("%s", s) (or "%v") where the single verb consumes a value that is
already a string. The formatting call allocates a new string only to reproduce its argument.
Rationale
When the format string is exactly one %s/%v verb and the operand is already a string, the
result equals the operand. The Sprintf call adds an allocation, a format-parse, and visual
noise without changing the value. Using the value directly is clearer and cheaper. Numeric
verbs (%d), surrounding literal text ([%s]), and multiple verbs all do real work and are
not reported.
// Bad
msg := fmt.Sprintf("%s", name)
// Good
msg := name