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:
- 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.
- 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
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 · ◷
Summary
The new
sortsliceanalyzer decides whether aX.Slice(...)/X.SliceStable(...)call targets the stdlibsortpackage purely by the syntactic identifier textX == "sort". It never consultspass.TypesInfoto confirmXactually resolves to the importedsortpackage. This makessortslicethe 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).pkg/linters/sortslice/sortslice.go:54-57Evidence
Current matching logic (
sortslice.go:54-57):This is a string comparison on the identifier name only. Two concrete defects follow:
sortwith aSlice/SliceStablemethod is flagged even though it has nothing to do with the stdlib. Example that would be wrongly reported:Likewise a non-stdlib package aliased to the name
sort(import sort "example.com/mysort") is wrongly flagged.sortpackage imported under an alias is silently missed:Self-inconsistency: every other custom linter that matches a stdlib-package call resolves the package via
pass.TypesInfoand compares the import path, not the spelled name:fmterrorfnoverbs.go:90-98ObjectOf(pkgIdent).(*types.PkgName).Imported().Path() == "fmt"jsonmarshalignoredeerror.go:88pass.TypesInfo.Uses[ident]strconvparseignorederror.go:75pass.TypesInfo.Uses[ident]ossetenvlibrary.go:71-73pass.TypesInfo.Uses[fun.Sel]sortsliceis the lone exception.Recommendation
Mirror the proven
fmterrorfnoverbs.isFmtErrorftemplate — resolve the package object and compare the import path:This removes the FP on shadowing identifiers and the FN on aliased imports in one change, and brings
sortslicein line with the rest of the linter suite. (sortslicealready builds withinspect, sopass.TypesInfois available.)Validation checklist
sortunder an alias (import s "sort"; s.Slice(...)) — want diagnostic; a non-sort identifier namedsortcalling.Slice(...)— no diagnosticsort.Strings/sort.Intsgood cases still passgo test ./pkg/linters/sortslice/...greenEffort: Small (single-file precision fix + 2 testdata cases). Mirrors prior single-file precision fixes that landed within ~1 day (e.g. #37741, #37492).