| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package modernize |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/token" |
| 10 | "go/types" |
| 11 | |
| 12 | "golang.org/x/tools/go/analysis" |
| 13 | "golang.org/x/tools/go/analysis/passes/inspect" |
| 14 | "golang.org/x/tools/internal/analysis/analyzerutil" |
| 15 | typeindexanalyzer "golang.org/x/tools/internal/analysis/typeindex" |
| 16 | "golang.org/x/tools/internal/astutil" |
| 17 | "golang.org/x/tools/internal/refactor" |
| 18 | "golang.org/x/tools/internal/typesinternal/typeindex" |
| 19 | "golang.org/x/tools/internal/versions" |
| 20 | ) |
| 21 | |
| 22 | // (Not to be confused with go/analysis/passes/sortslice.) |
| 23 | var SlicesSortAnalyzer = &analysis.Analyzer{ |
| 24 | Name: "slicessort", |
| 25 | Doc: analyzerutil.MustExtractDoc(doc, "slicessort"), |
| 26 | Requires: []*analysis.Analyzer{ |
| 27 | inspect.Analyzer, |
| 28 | typeindexanalyzer.Analyzer, |
| 29 | }, |
| 30 | Run: slicessort, |
| 31 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#slicessort", |
| 32 | } |
| 33 | |
| 34 | // The slicessort pass replaces sort.Slice(slice, less) with |
| 35 | // slices.Sort(slice) when slice is a []T and less is a FuncLit |
| 36 | // equivalent to cmp.Ordered[T]. |
| 37 | // |
| 38 | // sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) |
| 39 | // => slices.Sort(s) |
| 40 | // |
| 41 | // There is no slices.SortStable. |
| 42 | // |
| 43 | // TODO(adonovan): support |
| 44 | // |
| 45 | // - sort.Slice(s, func(i, j int) bool { return s[i] ... s[j] }) |
| 46 | // -> slices.SortFunc(s, func(x, y T) int { return x ... y }) |
| 47 | // iff all uses of i, j can be replaced by s[i], s[j] and "<" can be replaced with cmp.Compare. |
| 48 | // |
| 49 | // - As above for sort.SliceStable -> slices.SortStableFunc. |
| 50 | // |
| 51 | // - sort.Sort(x) where x has a named slice type whose Less method is the natural order. |
| 52 | // -> sort.Slice(x) |
| 53 | func slicessort(pass *analysis.Pass) (any, error) { |
| 54 | // Skip the analyzer in packages where its |
| 55 | // fixes would create an import cycle. |
| 56 | if within(pass, "slices", "sort", "runtime") { |
| 57 | return nil, nil |
| 58 | } |
| 59 | |
| 60 | var ( |
| 61 | info = pass.TypesInfo |
| 62 | index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) |
| 63 | sortSlice = index.Object("sort", "Slice") |
| 64 | ) |
| 65 | for curCall := range index.Calls(sortSlice) { |
| 66 | call := curCall.Node().(*ast.CallExpr) |
| 67 | if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 { |
| 68 | sig := info.Types[lit.Type].Type.(*types.Signature) |
| 69 | |
| 70 | // Have: sort.Slice(s, func(i, j int) bool { return ... }) |
| 71 | s := call.Args[0] |
| 72 | i := sig.Params().At(0) |
| 73 | j := sig.Params().At(1) |
| 74 | |
| 75 | if ret, ok := lit.Body.List[0].(*ast.ReturnStmt); ok { |
| 76 | if compare, ok := ret.Results[0].(*ast.BinaryExpr); ok && compare.Op == token.LSS { |
| 77 | // isIndex reports whether e is s[v]. |
| 78 | isIndex := func(e ast.Expr, v *types.Var) bool { |
| 79 | index, ok := e.(*ast.IndexExpr) |
| 80 | return ok && |
| 81 | astutil.EqualSyntax(index.X, s) && |
| 82 | is[*ast.Ident](index.Index) && |
| 83 | info.Uses[index.Index.(*ast.Ident)] == v |
| 84 | } |
| 85 | file := astutil.EnclosingFile(curCall) |
| 86 | if isIndex(compare.X, i) && isIndex(compare.Y, j) && |
| 87 | analyzerutil.FileUsesGoVersion(pass, file, versions.Go1_21) { |
| 88 | // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) |
| 89 | |
| 90 | prefix, importEdits := refactor.AddImport( |
| 91 | info, file, "slices", "slices", "Sort", call.Pos()) |
| 92 | |
| 93 | pass.Report(analysis.Diagnostic{ |
| 94 | // Highlight "sort.Slice". |
| 95 | Pos: call.Fun.Pos(), |
| 96 | End: call.Fun.End(), |
| 97 | Message: "sort.Slice can be modernized using slices.Sort", |
| 98 | SuggestedFixes: []analysis.SuggestedFix{{ |
| 99 | Message: "Replace sort.Slice call by slices.Sort", |
| 100 | TextEdits: append(importEdits, []analysis.TextEdit{ |
| 101 | { |
| 102 | // Replace sort.Slice with slices.Sort. |
| 103 | Pos: call.Fun.Pos(), |
| 104 | End: call.Fun.End(), |
| 105 | NewText: []byte(prefix + "Sort"), |
| 106 | }, |
| 107 | { |
| 108 | // Eliminate FuncLit. |
| 109 | Pos: call.Args[0].End(), |
| 110 | End: call.Rparen, |
| 111 | }, |
| 112 | }...), |
| 113 | }}, |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return nil, nil |
| 121 | } |