Skip to content

sortslice precision: match sort.Slice/SliceStable via package identity (pass.TypesInfo), not the syntactic identifier name "sort [Content truncated due to length] #38029

Description

@github-actions

Summary

The new sortslice analyzer decides whether a X.Slice(...) / X.SliceStable(...) call targets the stdlib sort package purely by the syntactic identifier text X == "sort". It never consults pass.TypesInfo to confirm X actually resolves to the imported sort package. This makes sortslice the only package-matching custom linter that skips type-based package verification, so it is simultaneously prone to false positives (shadowing) and false negatives (aliased import).

  • Location: pkg/linters/sortslice/sortslice.go:54-57
  • Type: Analyzer precision / self-inconsistency with sibling linters

Evidence

Current matching logic (sortslice.go:54-57):

pkgIdent, ok := sel.X.(*ast.Ident)
if !ok || pkgIdent.Name != "sort" {
    return
}

This is a string comparison on the identifier name only. Two concrete defects follow:

  1. False positive (forward-looking robustness): any value named sort with a Slice/SliceStable method is flagged even though it has nothing to do with the stdlib. Example that would be wrongly reported:
sort := newSorter()        // a local of some custom type
sort.Slice(items, less)    // FALSE POSITIVE: not the stdlib sort package

Likewise a non-stdlib package aliased to the name sort (import sort "example.com/mysort") is wrongly flagged.

  1. False negative: the stdlib sort package imported under an alias is silently missed:
import stdsort "sort"
stdsort.Slice(items, less)  // FALSE NEGATIVE: real sort.Slice, not flagged

Self-inconsistency: every other custom linter that matches a stdlib-package call resolves the package via pass.TypesInfo and compares the import path, not the spelled name:

Linter Verification
fmterrorfnoverbs.go:90-98 ObjectOf(pkgIdent).(*types.PkgName).Imported().Path() == "fmt"
jsonmarshalignoredeerror.go:88 pass.TypesInfo.Uses[ident]
strconvparseignorederror.go:75 pass.TypesInfo.Uses[ident]
ossetenvlibrary.go:71-73 pass.TypesInfo.Uses[fun.Sel]

sortslice is the lone exception.

Recommendation

Mirror the proven fmterrorfnoverbs.isFmtErrorf template — resolve the package object and compare the import path:

pkgIdent, ok := sel.X.(*ast.Ident)
if !ok {
    return
}
pkgName, ok := pass.TypesInfo.ObjectOf(pkgIdent).(*types.PkgName)
if !ok || pkgName.Imported().Path() != "sort" {
    return
}

This removes the FP on shadowing identifiers and the FN on aliased imports in one change, and brings sortslice in line with the rest of the linter suite. (sortslice already builds with inspect, so pass.TypesInfo is available.)

Validation checklist

  • Add testdata cases: stdlib sort under an alias (import s "sort"; s.Slice(...)) — want diagnostic; a non-sort identifier named sort calling .Slice(...)no diagnostic
  • Existing sort.Strings/sort.Ints good cases still pass
  • go test ./pkg/linters/sortslice/... green

Effort: Small (single-file precision fix + 2 testdata cases). Mirrors prior single-file precision fixes that landed within ~1 day (e.g. #37741, #37492).

Generated by 🤖 Sergo - Serena Go Expert · 239.4 AIC · ⌖ 14.4 AIC · ⊞ 6.5K ·

  • expires on Jun 15, 2026, 9:12 PM UTC-08:00

Metadata

Metadata

Labels

cookieIssue Monster Loves Cookies!sergo

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions