-
Notifications
You must be signed in to change notification settings - Fork 418
sortslice: match stdlib sort calls by type identity, not identifier text #38053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
cbcd509
2c43a51
135db56
669902f
43099fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ package sortslice | |
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
| "go/types" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
| "golang.org/x/tools/go/analysis/passes/inspect" | ||
|
|
@@ -52,7 +53,16 @@ func run(pass *analysis.Pass) (any, error) { | |
| return | ||
| } | ||
| pkgIdent, ok := sel.X.(*ast.Ident) | ||
| if !ok || pkgIdent.Name != "sort" { | ||
| if !ok { | ||
| return | ||
| } | ||
| obj := pass.TypesInfo.ObjectOf(pkgIdent) | ||
| // ObjectOf can be nil when type information is incomplete. | ||
| if obj == nil { | ||
| return | ||
| } | ||
| pkgName, ok := obj.(*types.PkgName) | ||
| if !ok || pkgName.Imported().Path() != "sort" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The 💡 Alternative: dynamic message using the local aliasIf you prefer the message to match the user's actual code: case "Slice":
pass.ReportRangef(call, "%s.Slice is not type-safe; use slices.SortFunc instead", pkgIdent.Name)
case "SliceStable":
pass.ReportRangef(call, "%s.SliceStable is not type-safe; use slices.SortStableFunc instead", pkgIdent.Name)With |
||
| return | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package sortslice | ||
|
|
||
| import s "sort" | ||
|
|
||
| func BadSliceAliasedImport(items []string) { | ||
| s.Slice(items, func(i, j int) bool { return items[i] < items[j] }) // want `sort\.Slice is not type-safe` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/diagnose] The |
||
| } | ||
|
|
||
| func BadSliceStableAliasedImport(items []string) { | ||
| s.SliceStable(items, func(i, j int) bool { return items[i] < items[j] }) // want `sort\.SliceStable is not type-safe` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package sortslice | ||
|
|
||
| type customSort struct{} | ||
|
|
||
| func (customSort) Slice(_ []string, _ func(i, j int) bool) {} | ||
| func (customSort) SliceStable(_ []string, _ func(i, j int) bool) {} | ||
|
|
||
| func GoodShadowedSortIdentifier(items []string) { | ||
| sort := customSort{} | ||
| sort.Slice(items, func(i, j int) bool { return items[i] < items[j] }) | ||
| sort.SliceStable(items, func(i, j int) bool { return items[i] < items[j] }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing nil guard on
pass.TypesInfoitself: the code callspass.TypesInfo.ObjectOf(...)without first checkingpass.TypesInfo != nil, so a non-standard or embedded driver that omits type info would panic here rather than skip gracefully.💡 Suggested fix
Add an early guard before the
ObjectOfcall:The existing
if obj == nilcheck (line 61) only protects againstObjectOfreturning nil on an initialisedTypesInfo— it does not prevent a nil-pointer dereference onpass.TypesInfoitself. Standard drivers (go vet, golangci-lint,analysistest) always populateTypesInfo, so this will not trigger in normal CI; but it is a latent panic for any embedded or custom driver that skips type-checking. Adding the guard is a one-liner and is consistent with the defensive style already present in this function.