Debian Codegraph @

gopls/internal/analysis/simplifyslice/simplifyslice.go

raw symbols SCIP
1 // Copyright 2023 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 simplifyslice
6
7 import (
8 "bytes"
9 _ "embed"
10 "fmt"
11 "go/ast"
12 "go/printer"
13 "go/token"
14
15 "golang.org/x/tools/go/analysis"
16 "golang.org/x/tools/go/analysis/passes/inspect"
17 "golang.org/x/tools/go/ast/inspector"
18 "golang.org/x/tools/internal/analysis/analyzerutil"
19 )
20
21 //go:embed doc.go
22 var doc string
23
24 var Analyzer = &analysis.Analyzer{
25 Name: "simplifyslice",
26 Doc: analyzerutil.MustExtractDoc(doc, "simplifyslice"),
27 Requires: []*analysis.Analyzer{inspect.Analyzer},
28 Run: run,
29 URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifyslice",
30 }
31
32 // Note: We could also simplify slice expressions of the form s[0:b] to s[:b]
33 // but we leave them as is since sometimes we want to be very explicit
34 // about the lower bound.
35 // An example where the 0 helps:
36 // x, y, z := b[0:2], b[2:4], b[4:6]
37 // An example where it does not:
38 // x, y := b[:n], b[n:]
39
40 func run(pass *analysis.Pass) (any, error) {
41 // Gather information whether file is generated or not
42 generated := make(map[*token.File]bool)
43 for _, file := range pass.Files {
44 if ast.IsGenerated(file) {
45 generated[pass.Fset.File(file.FileStart)] = true
46 }
47 }
48
49 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
50 nodeFilter := []ast.Node{
51 (*ast.SliceExpr)(nil),
52 }
53 inspect.Preorder(nodeFilter, func(n ast.Node) {
54 if _, ok := generated[pass.Fset.File(n.Pos())]; ok {
55 return // skip checking if it's generated code
56 }
57
58 expr := n.(*ast.SliceExpr)
59 // - 3-index slices always require the 2nd and 3rd index
60 if expr.Max != nil {
61 return
62 }
63 s, ok := expr.X.(*ast.Ident)
64 // the array/slice object is a single, resolved identifier
65 if !ok || s.Obj == nil {
66 return
67 }
68 call, ok := expr.High.(*ast.CallExpr)
69 // the high expression is a function call with a single argument
70 if !ok || len(call.Args) != 1 || call.Ellipsis.IsValid() {
71 return
72 }
73 fun, ok := call.Fun.(*ast.Ident)
74 // the function called is "len" and it is not locally defined; and
75 // because we don't have dot imports, it must be the predefined len()
76 if !ok || fun.Name != "len" || fun.Obj != nil {
77 return
78 }
79 arg, ok := call.Args[0].(*ast.Ident)
80 // the len argument is the array/slice object
81 if !ok || arg.Obj != s.Obj {
82 return
83 }
84 var b bytes.Buffer
85 printer.Fprint(&b, pass.Fset, expr.High) // ignore error
86 pass.Report(analysis.Diagnostic{
87 Pos: expr.High.Pos(),
88 End: expr.High.End(),
89 Message: fmt.Sprintf("unneeded: %s", b.String()),
90 SuggestedFixes: []analysis.SuggestedFix{{
91 Message: fmt.Sprintf("Remove '%s'", b.String()),
92 TextEdits: []analysis.TextEdit{{
93 Pos: expr.High.Pos(),
94 End: expr.High.End(),
95 NewText: []byte{},
96 }},
97 }},
98 })
99 })
100 return nil, nil
101 }