Omit Default Slice Index

ID

go.omit_default_slice_index

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 slice expression s[n:len(s)] whose high index is exactly len(s) on the same operand being sliced.

Rationale

The high index of a two-index slice defaults to the length of the operand, so s[n:len(s)] is identical to the shorter s[n:]. Spelling out the default adds noise. A len of a different operand, an explicit numeric bound, or a three-index (full) slice — whose high index cannot be omitted — are left alone.

func fn(s []int) {
    _ = s[1:len(s)] // FLAW — same as s[1:]
    _ = s[1:]       // OK
}

Remediation

Drop the high index: write s[n:] instead of s[n:len(s)].