|
| 1 | +// Package lenstringzero implements a Go analysis linter that flags len(s) == 0 |
| 2 | +// and len(s) != 0 comparisons on string values that should use == "" or != "" instead. |
| 3 | +package lenstringzero |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "go/ast" |
| 8 | + "go/token" |
| 9 | + "go/types" |
| 10 | + |
| 11 | + "golang.org/x/tools/go/analysis" |
| 12 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 13 | + "golang.org/x/tools/go/ast/inspector" |
| 14 | + |
| 15 | + "github.com/github/gh-aw/pkg/linters/internal/filecheck" |
| 16 | +) |
| 17 | + |
| 18 | +var Analyzer = &analysis.Analyzer{ |
| 19 | + Name: "lenstringzero", |
| 20 | + Doc: "reports len(s) == 0 and len(s) != 0 comparisons on string values that should use == \"\" or != \"\" instead", |
| 21 | + URL: "https://github.com/github/gh-aw/tree/main/pkg/linters/lenstringzero", |
| 22 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 23 | + Run: run, |
| 24 | +} |
| 25 | + |
| 26 | +func run(pass *analysis.Pass) (any, error) { |
| 27 | + insp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 28 | + if !ok { |
| 29 | + return nil, fmt.Errorf("inspect analyzer result has unexpected type %T", pass.ResultOf[inspect.Analyzer]) |
| 30 | + } |
| 31 | + |
| 32 | + nodeFilter := []ast.Node{(*ast.BinaryExpr)(nil)} |
| 33 | + |
| 34 | + insp.Preorder(nodeFilter, func(n ast.Node) { |
| 35 | + expr, ok := n.(*ast.BinaryExpr) |
| 36 | + if !ok { |
| 37 | + return |
| 38 | + } |
| 39 | + if expr.Op != token.EQL && expr.Op != token.NEQ { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + pos := pass.Fset.PositionFor(expr.Pos(), false) |
| 44 | + if filecheck.IsTestFile(pos.Filename) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + var lenArg ast.Expr |
| 49 | + if isLenCall(expr.X) && isIntZero(expr.Y) { |
| 50 | + lenArg = lenCallArg(expr.X) |
| 51 | + } else if isIntZero(expr.X) && isLenCall(expr.Y) { |
| 52 | + lenArg = lenCallArg(expr.Y) |
| 53 | + } |
| 54 | + if lenArg == nil { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + t := pass.TypesInfo.TypeOf(lenArg) |
| 59 | + if t == nil { |
| 60 | + return |
| 61 | + } |
| 62 | + basic, ok := t.Underlying().(*types.Basic) |
| 63 | + if !ok || basic.Kind() != types.String { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + op := expr.Op.String() |
| 68 | + var cmpVerb string |
| 69 | + if expr.Op == token.EQL { |
| 70 | + cmpVerb = "empty" |
| 71 | + } else { |
| 72 | + cmpVerb = "non-empty" |
| 73 | + } |
| 74 | + pass.ReportRangef(expr, |
| 75 | + `use s %s "" to check for %s string instead of len(s) %s 0`, |
| 76 | + op, cmpVerb, op) |
| 77 | + }) |
| 78 | + |
| 79 | + return nil, nil |
| 80 | +} |
| 81 | + |
| 82 | +func isLenCall(expr ast.Expr) bool { |
| 83 | + call, ok := expr.(*ast.CallExpr) |
| 84 | + if !ok || len(call.Args) != 1 { |
| 85 | + return false |
| 86 | + } |
| 87 | + ident, ok := call.Fun.(*ast.Ident) |
| 88 | + return ok && ident.Name == "len" |
| 89 | +} |
| 90 | + |
| 91 | +func lenCallArg(expr ast.Expr) ast.Expr { |
| 92 | + return expr.(*ast.CallExpr).Args[0] |
| 93 | +} |
| 94 | + |
| 95 | +func isIntZero(expr ast.Expr) bool { |
| 96 | + lit, ok := expr.(*ast.BasicLit) |
| 97 | + return ok && lit.Kind == token.INT && lit.Value == "0" |
| 98 | +} |
0 commit comments