Simplify Make Slice
ID |
go.simplify_make_slice |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
CWE:1164, code-style, readability |
Description
Reports a range loop that copies every element of one slice onto another with append, where a
single append with a spread would do the whole job.
Rationale
dst = append(dst, src…) concatenates two slices in one call. Spelling it out as a loop is more
code, slower (no bulk growth) and obscures the intent. The rule fires only when source and
destination resolve to the same slice element type and the destination is not the slice being
ranged; differing element types or unresolved types are left alone.
func fn(src, dst []int) {
for _, v := range src { // FLAW — dst = append(dst, src...)
dst = append(dst, v)
}
_ = dst
}