Unnecessary fmt.Sprint

ID

go.unnecessary_fmt_sprint

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

Go

Tags

best-practice, code-style

Description

Reports fmt.Sprint(s) called with a single argument that is already a string. The call formats its operand into a string, which for a string operand simply returns a copy.

Rationale

fmt.Sprint exists to turn arbitrary operands into their string form. Passing it a value that is already a string returns the same text after an allocation and a trip through the formatter, so the call accomplishes nothing. Converting a non-string operand (fmt.Sprint(n)) or joining several operands (fmt.Sprint(a, b)) is a real use and is not reported.

// Bad
s := fmt.Sprint(name)

// Good
s := name

Remediation

Drop the fmt.Sprint wrapper and use the string value directly.