Inefficient String Comparison

ID

go.inefficient_string_comparison

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Go

Tags

efficiency, performance

Description

Reports a case-insensitive string comparison written as strings.ToLower(a) == strings.ToLower(b) (or with strings.ToUpper, and the != operator). The idiomatic, allocation-free form is strings.EqualFold(a, b).

Rationale

Each strings.ToLower / strings.ToUpper call allocates a fresh copy of its entire operand before the comparison begins, so this idiom does two heap allocations and folds the whole string even when the inputs differ in the first byte. strings.EqualFold folds case lazily while it compares, allocating nothing and stopping at the first mismatch.

strings.ToLower(a) == strings.ToLower(b)  // FLAW
strings.ToUpper(a) != strings.ToUpper(b)  // FLAW
strings.ToLower(a) == b                   // OK — only one folded side
strings.EqualFold(a, b)                   // OK — already idiomatic

Remediation

Replace the comparison with strings.EqualFold(a, b) for ==, or !strings.EqualFold(a, b) for !=.