Use strings.ReplaceAll

ID

go.strings_replaceall

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

Go

Tags

best-practice, code-style

Description

Reports strings.Replace(s, old, new, -1), where the count argument -1 means "replace every occurrence". The standard library expresses exactly this as strings.ReplaceAll(s, old, new).

Rationale

The -1 count is a magic sentinel that readers must recognise as "all". strings.ReplaceAll names that intent, drops the trailing argument, and is harder to get wrong (a stray 0 or 1 count silently changes the behaviour). A positive count replaces only the first N matches and is a deliberate, different operation.

// Bad
out := strings.Replace(s, "a", "b", -1)

// Good
out := strings.ReplaceAll(s, "a", "b")

Remediation

Replace strings.Replace(s, old, new, -1) with strings.ReplaceAll(s, old, new).