Drop Blank Identifier
ID |
go.drop_blank_identifier |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
Go |
Tags |
best-practice, code-style |
Description
Reports a _ blank identifier in a range clause that the language does not require: a trailing
blank value (for k, _ := range m) or a lone blank (for _ = range ch).
Rationale
Go lets you omit the range value entirely (for k := range m) and, since Go 1.4, omit the loop
variable altogether (for range ch). Keeping a _ where the syntax already allows nothing is just
noise. A blank in the key position of a two-variable range is left alone, because the value still
needs its comma and the placeholder is genuinely required there.
// Bad
for k, _ := range m { ... }
for _ = range ch { ... }
// Good
for k := range m { ... }
for range ch { ... }