| 1 | // Copyright 2019 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 sortslice defines an Analyzer that checks for calls |
| 6 | // to sort.Slice that do not use a slice type as first argument. |
| 7 | package sortslice |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "fmt" |
| 12 | "go/ast" |
| 13 | "go/format" |
| 14 | "go/types" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysis" |
| 17 | "golang.org/x/tools/go/analysis/passes/inspect" |
| 18 | "golang.org/x/tools/go/ast/inspector" |
| 19 | "golang.org/x/tools/go/types/typeutil" |
| 20 | "golang.org/x/tools/internal/typesinternal" |
| 21 | ) |
| 22 | |
| 23 | const Doc = `check the argument type of sort.Slice |
| 24 | |
| 25 | sort.Slice requires an argument of a slice type. Check that |
| 26 | the interface{} value passed to sort.Slice is actually a slice.` |
| 27 | |
| 28 | var Analyzer = &analysis.Analyzer{ |
| 29 | Name: "sortslice", |
| 30 | Doc: Doc, |
| 31 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/sortslice", |
| 32 | Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 33 | Run: run, |
| 34 | } |
| 35 | |
| 36 | func run(pass *analysis.Pass) (any, error) { |
| 37 | if !typesinternal.Imports(pass.Pkg, "sort") { |
| 38 | return nil, nil // doesn't directly import sort |
| 39 | } |
| 40 | |
| 41 | inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 42 | |
| 43 | nodeFilter := []ast.Node{ |
| 44 | (*ast.CallExpr)(nil), |
| 45 | } |
| 46 | |
| 47 | inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 48 | call := n.(*ast.CallExpr) |
| 49 | obj := typeutil.Callee(pass.TypesInfo, call) |
| 50 | if !typesinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable", "SliceIsSorted") { |
| 51 | return |
| 52 | } |
| 53 | callee := obj.(*types.Func) |
| 54 | |
| 55 | arg := call.Args[0] |
| 56 | typ := pass.TypesInfo.Types[arg].Type |
| 57 | |
| 58 | if tuple, ok := typ.(*types.Tuple); ok { |
| 59 | typ = tuple.At(0).Type() // special case for Slice(f(...)) |
| 60 | } |
| 61 | |
| 62 | switch typ.Underlying().(type) { |
| 63 | case *types.Slice, *types.Interface: |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | // Restore typ to the original type, we may unwrap the tuple above, |
| 68 | // typ might not be the type of arg. |
| 69 | typ = pass.TypesInfo.Types[arg].Type |
| 70 | |
| 71 | var fixes []analysis.SuggestedFix |
| 72 | switch v := typ.Underlying().(type) { |
| 73 | case *types.Array: |
| 74 | var buf bytes.Buffer |
| 75 | format.Node(&buf, pass.Fset, &ast.SliceExpr{ |
| 76 | X: arg, |
| 77 | Slice3: false, |
| 78 | Lbrack: arg.End() + 1, |
| 79 | Rbrack: arg.End() + 3, |
| 80 | }) |
| 81 | fixes = append(fixes, analysis.SuggestedFix{ |
| 82 | Message: "Get a slice of the full array", |
| 83 | TextEdits: []analysis.TextEdit{{ |
| 84 | Pos: arg.Pos(), |
| 85 | End: arg.End(), |
| 86 | NewText: buf.Bytes(), |
| 87 | }}, |
| 88 | }) |
| 89 | case *types.Pointer: |
| 90 | _, ok := v.Elem().Underlying().(*types.Slice) |
| 91 | if !ok { |
| 92 | break |
| 93 | } |
| 94 | var buf bytes.Buffer |
| 95 | format.Node(&buf, pass.Fset, &ast.StarExpr{ |
| 96 | X: arg, |
| 97 | }) |
| 98 | fixes = append(fixes, analysis.SuggestedFix{ |
| 99 | Message: "Dereference the pointer to the slice", |
| 100 | TextEdits: []analysis.TextEdit{{ |
| 101 | Pos: arg.Pos(), |
| 102 | End: arg.End(), |
| 103 | NewText: buf.Bytes(), |
| 104 | }}, |
| 105 | }) |
| 106 | case *types.Signature: |
| 107 | if v.Params().Len() != 0 || v.Results().Len() != 1 { |
| 108 | break |
| 109 | } |
| 110 | if _, ok := v.Results().At(0).Type().Underlying().(*types.Slice); !ok { |
| 111 | break |
| 112 | } |
| 113 | var buf bytes.Buffer |
| 114 | format.Node(&buf, pass.Fset, &ast.CallExpr{ |
| 115 | Fun: arg, |
| 116 | }) |
| 117 | fixes = append(fixes, analysis.SuggestedFix{ |
| 118 | Message: "Call the function", |
| 119 | TextEdits: []analysis.TextEdit{{ |
| 120 | Pos: arg.Pos(), |
| 121 | End: arg.End(), |
| 122 | NewText: buf.Bytes(), |
| 123 | }}, |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | pass.Report(analysis.Diagnostic{ |
| 128 | Pos: call.Pos(), |
| 129 | End: call.End(), |
| 130 | Message: fmt.Sprintf("%s's argument must be a slice; is called with %s", callee.FullName(), typ.String()), |
| 131 | SuggestedFixes: fixes, |
| 132 | }) |
| 133 | }) |
| 134 | return nil, nil |
| 135 | } |