From a9bf6fdf9803c2872968caff08802cf427eb875c Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Feb 2025 11:26:00 -0500 Subject: [PATCH] gopls/internal/analysis/modernize: remove SortStable 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 LUCI-TryBot-Result: Go LUCI --- .../internal/analysis/modernize/sortslice.go | 21 +++++++++---------- .../testdata/src/sortslice/sortslice.go | 2 -- .../src/sortslice/sortslice.go.golden | 4 ---- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 7f590eefc32..4f856d39c33 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -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" @@ -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) @@ -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) @@ -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. diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go index fce3e006328..53d15746839 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go @@ -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 }) { diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden index 176ae66d204..d97636fd311 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden @@ -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 }) {