Skip to content

Commit

Permalink
gopls/internal/analysis/modernize: remove SortStable
Browse files Browse the repository at this point in the history
Remove the modernization from sort.SliceStable to slices.SortStable.

There is no slices.SortStable.

Change-Id: I55e6c6848aa2708976d35ceabab73e7b55da1d1f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/647735
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
jba committed Feb 7, 2025
1 parent 4d1de70 commit a9bf6fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
21 changes: 10 additions & 11 deletions gopls/internal/analysis/modernize/sortslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go/ast"
"go/token"
"go/types"
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
Expand All @@ -25,13 +24,15 @@ import (
// sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
// => slices.Sort(s)
//
// It also supports the SliceStable variant.
// There is no slices.SortStable.
//
// TODO(adonovan): support
//
// - sort.Slice(s, func(i, j int) bool { return s[i] ... s[j] })
// -> slices.SortFunc(s, func(x, y int) bool { return x ... y })
// iff all uses of i, j can be replaced by s[i], s[j].
// -> slices.SortFunc(s, func(x, y T) int { return x ... y })
// iff all uses of i, j can be replaced by s[i], s[j] and "<" can be replaced with cmp.Compare.
//
// - As above for sort.SliceStable -> slices.SortStableFunc.
//
// - sort.Sort(x) where x has a named slice type whose Less method is the natural order.
// -> sort.Slice(x)
Expand All @@ -43,13 +44,11 @@ func sortslice(pass *analysis.Pass) {
info := pass.TypesInfo

check := func(file *ast.File, call *ast.CallExpr) {
// call to sort.Slice{,Stable}?
// call to sort.Slice?
obj := typeutil.Callee(info, call)
if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable") {
if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice") {
return
}
stable := cond(strings.HasSuffix(obj.Name(), "Stable"), "Stable", "")

if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 {
sig := info.Types[lit.Type].Type.(*types.Signature)

Expand Down Expand Up @@ -78,15 +77,15 @@ func sortslice(pass *analysis.Pass) {
Pos: call.Fun.Pos(),
End: call.Fun.End(),
Category: "sortslice",
Message: fmt.Sprintf("sort.Slice%[1]s can be modernized using slices.Sort%[1]s", stable),
Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"),
SuggestedFixes: []analysis.SuggestedFix{{
Message: fmt.Sprintf("Replace sort.Slice%[1]s call by slices.Sort%[1]s", stable),
Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"),
TextEdits: append(importEdits, []analysis.TextEdit{
{
// Replace sort.Slice with slices.Sort.
Pos: call.Fun.Pos(),
End: call.Fun.End(),
NewText: []byte(slicesName + ".Sort" + stable),
NewText: []byte(slicesName + ".Sort"),
},
{
// Eliminate FuncLit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ type myint int

func _(s []myint) {
sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort"

sort.SliceStable(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.SliceStable can be modernized using slices.SortStable"
}

func _(x *struct{ s []int }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import "slices"

import "slices"

import "slices"

import "sort"

type myint int

func _(s []myint) {
slices.Sort(s) // want "sort.Slice can be modernized using slices.Sort"

slices.SortStable(s) // want "sort.SliceStable can be modernized using slices.SortStable"
}

func _(x *struct{ s []int }) {
Expand Down

0 comments on commit a9bf6fd

Please sign in to comment.