|
| 1 | +// Package lenstringsplit implements a Go analysis linter that flags |
| 2 | +// len(strings.Split(s, sep)) expressions with a provably non-empty separator |
| 3 | +// that allocate a []string just to count substrings. strings.Count(s, sep)+1 |
| 4 | +// achieves the same result for non-empty separators without the intermediate |
| 5 | +// allocation. |
| 6 | +package lenstringsplit |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "go/ast" |
| 11 | + "go/constant" |
| 12 | + "go/token" |
| 13 | + "go/types" |
| 14 | + |
| 15 | + "golang.org/x/tools/go/analysis" |
| 16 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 17 | + |
| 18 | + "github.com/github/gh-aw/pkg/linters/internal/astutil" |
| 19 | + "github.com/github/gh-aw/pkg/linters/internal/filecheck" |
| 20 | + "github.com/github/gh-aw/pkg/linters/internal/nolint" |
| 21 | +) |
| 22 | + |
| 23 | +// Analyzer is the len-strings-split analysis pass. |
| 24 | +var Analyzer = &analysis.Analyzer{ |
| 25 | + Name: "lenstringsplit", |
| 26 | + Doc: "reports len(strings.Split(s, sep)) expressions with a provably non-empty separator that allocate a []string just to count substrings; use strings.Count(s, sep)+1 instead", |
| 27 | + URL: "https://github.com/github/gh-aw/tree/main/pkg/linters/lenstringsplit", |
| 28 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 29 | + Run: run, |
| 30 | +} |
| 31 | + |
| 32 | +func run(pass *analysis.Pass) (any, error) { |
| 33 | + insp, err := astutil.Inspector(pass) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + noLintLinesByFile := nolint.BuildLineIndex(pass, "lenstringsplit") |
| 38 | + |
| 39 | + nodeFilter := []ast.Node{(*ast.CallExpr)(nil)} |
| 40 | + |
| 41 | + insp.Preorder(nodeFilter, func(n ast.Node) { |
| 42 | + outer, ok := n.(*ast.CallExpr) |
| 43 | + if !ok { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if !isBuiltinLen(pass, outer) { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + if len(outer.Args) != 1 { |
| 52 | + return |
| 53 | + } |
| 54 | + inner, ok := outer.Args[0].(*ast.CallExpr) |
| 55 | + if !ok { |
| 56 | + return |
| 57 | + } |
| 58 | + if !isStringsSplit(pass, inner) { |
| 59 | + return |
| 60 | + } |
| 61 | + if !hasProvablyNonEmptySeparator(pass, inner) { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + pos := pass.Fset.PositionFor(outer.Pos(), false) |
| 66 | + if filecheck.IsTestFile(pos.Filename) { |
| 67 | + return |
| 68 | + } |
| 69 | + if nolint.HasDirective(pos, noLintLinesByFile) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + pass.Report(analysis.Diagnostic{ |
| 74 | + Pos: outer.Pos(), |
| 75 | + End: outer.End(), |
| 76 | + Message: "len(strings.Split(...)) allocates a []string just to count substrings; use strings.Count(...)+1 instead", |
| 77 | + SuggestedFixes: buildCountFix(pass, outer, inner), |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + return nil, nil |
| 82 | +} |
| 83 | + |
| 84 | +// isBuiltinLen reports whether call is an invocation of the builtin len function. |
| 85 | +func isBuiltinLen(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 86 | + ident, ok := call.Fun.(*ast.Ident) |
| 87 | + if !ok || ident.Name != "len" { |
| 88 | + return false |
| 89 | + } |
| 90 | + obj := pass.TypesInfo.Uses[ident] |
| 91 | + return obj == nil || obj == types.Universe.Lookup("len") |
| 92 | +} |
| 93 | + |
| 94 | +// isStringsSplit reports whether call is strings.Split from the standard |
| 95 | +// library "strings" package. |
| 96 | +func isStringsSplit(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 97 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 98 | + if !ok || sel.Sel.Name != "Split" { |
| 99 | + return false |
| 100 | + } |
| 101 | + return astutil.IsPkgSelector(pass, sel, "strings") |
| 102 | +} |
| 103 | + |
| 104 | +func hasProvablyNonEmptySeparator(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 105 | + if len(call.Args) != 2 { |
| 106 | + return false |
| 107 | + } |
| 108 | + if lit, ok := call.Args[1].(*ast.BasicLit); ok && lit.Kind == token.STRING { |
| 109 | + return lit.Value != `""` |
| 110 | + } |
| 111 | + tv, ok := pass.TypesInfo.Types[call.Args[1]] |
| 112 | + if !ok || tv.Value == nil || tv.Value.Kind() != constant.String { |
| 113 | + return false |
| 114 | + } |
| 115 | + return constant.StringVal(tv.Value) != "" |
| 116 | +} |
| 117 | + |
| 118 | +func buildCountFix(pass *analysis.Pass, outer, inner *ast.CallExpr) []analysis.SuggestedFix { |
| 119 | + if len(inner.Args) != 2 { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + sText := astutil.NodeText(pass.Fset, inner.Args[0]) |
| 124 | + sepText := astutil.NodeText(pass.Fset, inner.Args[1]) |
| 125 | + pkgText := splitPkgText(pass, inner) |
| 126 | + if sText == "" || sepText == "" || pkgText == "" { |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + return []analysis.SuggestedFix{{ |
| 131 | + Message: "Replace with strings.Count(...)+1", |
| 132 | + TextEdits: []analysis.TextEdit{{ |
| 133 | + Pos: outer.Pos(), |
| 134 | + End: outer.End(), |
| 135 | + NewText: fmt.Appendf(nil, "%s.Count(%s, %s)+1", pkgText, sText, sepText), |
| 136 | + }}, |
| 137 | + }} |
| 138 | +} |
| 139 | + |
| 140 | +func splitPkgText(pass *analysis.Pass, call *ast.CallExpr) string { |
| 141 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 142 | + if !ok { |
| 143 | + return "" |
| 144 | + } |
| 145 | + return astutil.NodeText(pass.Fset, sel.X) |
| 146 | +} |
0 commit comments