Use Sort Helper

ID

go.use_sort_helper

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Go

Tags

CWE:1164, code-style, readability

Description

Reports sort.Sort(sort.IntSlice(x)) (and the Float64Slice / StringSlice views) that wraps a plain slice in a sort view type only to pass it to sort.Sort.

Rationale

The sort package ships the dedicated in-place helpers sort.Ints, sort.Float64s and sort.Strings for exactly this case. They read more directly and avoid the extra view conversion. Only the three stock view types are flagged; user-defined slice types and values that already implement sort.Interface are left alone.

import "sort"

func fn(x []int) {
    sort.Sort(sort.IntSlice(x)) // FLAW — use sort.Ints(x)
    sort.Ints(x)                // OK
}

Remediation

Call the matching helper directly: sort.Ints(x), sort.Float64s(x) or sort.Strings(x).