|
| 1 | +// Package seenmapbool implements a Go analysis linter that flags "seen" maps |
| 2 | +// declared as map[string]bool (using true as sentinel) that should use |
| 3 | +// map[string]struct{} to avoid allocating a bool per entry. |
| 4 | +package seenmapbool |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "go/ast" |
| 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 | +// Analyzer is the seen-map-bool analysis pass. |
| 19 | +var Analyzer = &analysis.Analyzer{ |
| 20 | + Name: "seenmapbool", |
| 21 | + Doc: "reports map[string]bool used as a set (values always true) where map[string]struct{} should be used instead", |
| 22 | + URL: "https://github.com/github/gh-aw/tree/main/pkg/linters/seenmapbool", |
| 23 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 24 | + Run: run, |
| 25 | +} |
| 26 | + |
| 27 | +func run(pass *analysis.Pass) (any, error) { |
| 28 | + insp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 29 | + if !ok { |
| 30 | + return nil, fmt.Errorf("inspect analyzer result has unexpected type %T", pass.ResultOf[inspect.Analyzer]) |
| 31 | + } |
| 32 | + |
| 33 | + nodeFilter := []ast.Node{ |
| 34 | + (*ast.FuncDecl)(nil), |
| 35 | + (*ast.FuncLit)(nil), |
| 36 | + } |
| 37 | + |
| 38 | + insp.Preorder(nodeFilter, func(n ast.Node) { |
| 39 | + var body *ast.BlockStmt |
| 40 | + switch fn := n.(type) { |
| 41 | + case *ast.FuncDecl: |
| 42 | + if fn.Body == nil { |
| 43 | + return |
| 44 | + } |
| 45 | + pos := pass.Fset.PositionFor(fn.Pos(), false) |
| 46 | + if filecheck.IsTestFile(pos.Filename) { |
| 47 | + return |
| 48 | + } |
| 49 | + body = fn.Body |
| 50 | + case *ast.FuncLit: |
| 51 | + if fn.Body == nil { |
| 52 | + return |
| 53 | + } |
| 54 | + body = fn.Body |
| 55 | + } |
| 56 | + inspectBody(pass, body) |
| 57 | + }) |
| 58 | + |
| 59 | + return nil, nil |
| 60 | +} |
| 61 | + |
| 62 | +// inspectBody walks a function body and reports map[string]bool variables |
| 63 | +// that are only ever assigned the literal true (i.e., used as a set). |
| 64 | +func inspectBody(pass *analysis.Pass, body *ast.BlockStmt) { |
| 65 | + // Collect map[string]bool local variables defined in this scope. |
| 66 | + candidates := make(map[types.Object]ast.Node) // object -> declaration node for reporting |
| 67 | + |
| 68 | + // First pass: collect declarations of map[string]bool locals. |
| 69 | + ast.Inspect(body, func(n ast.Node) bool { |
| 70 | + switch stmt := n.(type) { |
| 71 | + case *ast.AssignStmt: |
| 72 | + // seen := make(map[string]bool) or seen := map[string]bool{} |
| 73 | + if stmt.Tok.String() != ":=" { |
| 74 | + return true |
| 75 | + } |
| 76 | + for i, lhs := range stmt.Lhs { |
| 77 | + if i >= len(stmt.Rhs) { |
| 78 | + break |
| 79 | + } |
| 80 | + ident, ok := lhs.(*ast.Ident) |
| 81 | + if !ok || ident.Name == "_" { |
| 82 | + continue |
| 83 | + } |
| 84 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 85 | + if obj == nil { |
| 86 | + continue |
| 87 | + } |
| 88 | + if isMapStringBool(pass.TypesInfo.TypeOf(ident)) && isMapStringBoolExpr(stmt.Rhs[i]) { |
| 89 | + candidates[obj] = ident |
| 90 | + } |
| 91 | + } |
| 92 | + case *ast.DeclStmt: |
| 93 | + // var seen map[string]bool |
| 94 | + genDecl, ok := stmt.Decl.(*ast.GenDecl) |
| 95 | + if !ok { |
| 96 | + return true |
| 97 | + } |
| 98 | + for _, spec := range genDecl.Specs { |
| 99 | + valSpec, ok := spec.(*ast.ValueSpec) |
| 100 | + if !ok { |
| 101 | + continue |
| 102 | + } |
| 103 | + for _, name := range valSpec.Names { |
| 104 | + if name.Name == "_" { |
| 105 | + continue |
| 106 | + } |
| 107 | + obj := pass.TypesInfo.ObjectOf(name) |
| 108 | + if obj == nil { |
| 109 | + continue |
| 110 | + } |
| 111 | + if isMapStringBool(pass.TypesInfo.TypeOf(name)) { |
| 112 | + candidates[obj] = name |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return true |
| 118 | + }) |
| 119 | + |
| 120 | + if len(candidates) == 0 { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + // Second pass: check that every write to these maps only assigns true. |
| 125 | + // If any non-true assignment is found, remove the map from candidates. |
| 126 | + nonSetMaps := make(map[types.Object]bool) |
| 127 | + |
| 128 | + ast.Inspect(body, func(n ast.Node) bool { |
| 129 | + assign, ok := n.(*ast.AssignStmt) |
| 130 | + if !ok { |
| 131 | + return true |
| 132 | + } |
| 133 | + for i, lhs := range assign.Lhs { |
| 134 | + indexExpr, ok := lhs.(*ast.IndexExpr) |
| 135 | + if !ok { |
| 136 | + continue |
| 137 | + } |
| 138 | + ident, ok := indexExpr.X.(*ast.Ident) |
| 139 | + if !ok { |
| 140 | + continue |
| 141 | + } |
| 142 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 143 | + if obj == nil { |
| 144 | + continue |
| 145 | + } |
| 146 | + if _, isCandidate := candidates[obj]; !isCandidate { |
| 147 | + continue |
| 148 | + } |
| 149 | + // Check the value being assigned. |
| 150 | + if i < len(assign.Rhs) { |
| 151 | + if !isBoolTrue(assign.Rhs[i]) { |
| 152 | + nonSetMaps[obj] = true |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return true |
| 157 | + }) |
| 158 | + |
| 159 | + // Report remaining candidates that are pure sets. |
| 160 | + for obj, declNode := range candidates { |
| 161 | + if nonSetMaps[obj] { |
| 162 | + continue |
| 163 | + } |
| 164 | + pass.ReportRangef( |
| 165 | + declNode, |
| 166 | + "map[string]bool %q used as a set; use map[string]struct{} to avoid allocating a bool per entry", |
| 167 | + obj.Name(), |
| 168 | + ) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// isMapStringBool returns true if t is map[string]bool. |
| 173 | +func isMapStringBool(t types.Type) bool { |
| 174 | + if t == nil { |
| 175 | + return false |
| 176 | + } |
| 177 | + m, ok := t.Underlying().(*types.Map) |
| 178 | + if !ok { |
| 179 | + return false |
| 180 | + } |
| 181 | + key, ok := m.Key().(*types.Basic) |
| 182 | + if !ok || key.Kind() != types.String { |
| 183 | + return false |
| 184 | + } |
| 185 | + val, ok := m.Elem().(*types.Basic) |
| 186 | + return ok && val.Kind() == types.Bool |
| 187 | +} |
| 188 | + |
| 189 | +// isMapStringBoolExpr reports whether expr is a make(map[string]bool, ...) call |
| 190 | +// or a map[string]bool{...} composite literal. |
| 191 | +func isMapStringBoolExpr(expr ast.Expr) bool { |
| 192 | + switch e := expr.(type) { |
| 193 | + case *ast.CallExpr: |
| 194 | + ident, ok := e.Fun.(*ast.Ident) |
| 195 | + if !ok || ident.Name != "make" { |
| 196 | + return false |
| 197 | + } |
| 198 | + if len(e.Args) == 0 { |
| 199 | + return false |
| 200 | + } |
| 201 | + return isMapStringBoolTypeExpr(e.Args[0]) |
| 202 | + case *ast.CompositeLit: |
| 203 | + return isMapStringBoolTypeExpr(e.Type) |
| 204 | + } |
| 205 | + return false |
| 206 | +} |
| 207 | + |
| 208 | +// isMapStringBoolTypeExpr reports whether the AST node represents map[string]bool. |
| 209 | +func isMapStringBoolTypeExpr(expr ast.Expr) bool { |
| 210 | + mapType, ok := expr.(*ast.MapType) |
| 211 | + if !ok { |
| 212 | + return false |
| 213 | + } |
| 214 | + keyIdent, ok := mapType.Key.(*ast.Ident) |
| 215 | + if !ok || keyIdent.Name != "string" { |
| 216 | + return false |
| 217 | + } |
| 218 | + valIdent, ok := mapType.Value.(*ast.Ident) |
| 219 | + return ok && valIdent.Name == "bool" |
| 220 | +} |
| 221 | + |
| 222 | +// isBoolTrue reports whether expr is the boolean literal true. |
| 223 | +func isBoolTrue(expr ast.Expr) bool { |
| 224 | + ident, ok := expr.(*ast.Ident) |
| 225 | + return ok && ident.Name == "true" |
| 226 | +} |
0 commit comments