|
| 1 | +package customlint |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "go/types" |
| 7 | + |
| 8 | + "golang.org/x/tools/go/analysis" |
| 9 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 10 | + "golang.org/x/tools/go/ast/inspector" |
| 11 | +) |
| 12 | + |
| 13 | +var cleanupAnalyzer = &analysis.Analyzer{ |
| 14 | + Name: "cleanup", |
| 15 | + Doc: "finds t.Cleanup calls where the closure captures a testing variable from the enclosing function", |
| 16 | + Requires: []*analysis.Analyzer{ |
| 17 | + inspect.Analyzer, |
| 18 | + }, |
| 19 | + Run: func(pass *analysis.Pass) (any, error) { |
| 20 | + return (&cleanupPass{pass: pass}).run() |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +type cleanupPass struct { |
| 25 | + pass *analysis.Pass |
| 26 | +} |
| 27 | + |
| 28 | +func (c *cleanupPass) run() (any, error) { |
| 29 | + in := c.pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 30 | + |
| 31 | + for cursor := range in.Root().Preorder((*ast.CallExpr)(nil)) { |
| 32 | + call := cursor.Node().(*ast.CallExpr) |
| 33 | + c.checkCall(call) |
| 34 | + } |
| 35 | + |
| 36 | + return nil, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (c *cleanupPass) checkCall(call *ast.CallExpr) { |
| 40 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 41 | + if !ok || sel.Sel.Name != "Cleanup" { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + recvType := c.pass.TypesInfo.TypeOf(sel.X) |
| 46 | + if recvType == nil || !isTestingType(recvType) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if len(call.Args) != 1 { |
| 51 | + return |
| 52 | + } |
| 53 | + funcLit, ok := call.Args[0].(*ast.FuncLit) |
| 54 | + if !ok { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + ast.Inspect(funcLit.Body, func(n ast.Node) bool { |
| 59 | + c.checkCleanupIdent(n, funcLit) |
| 60 | + return true |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +func (c *cleanupPass) checkCleanupIdent(n ast.Node, funcLit *ast.FuncLit) { |
| 65 | + ident, ok := n.(*ast.Ident) |
| 66 | + if !ok { |
| 67 | + return |
| 68 | + } |
| 69 | + obj := c.pass.TypesInfo.Uses[ident] |
| 70 | + if obj == nil { |
| 71 | + return |
| 72 | + } |
| 73 | + v, ok := obj.(*types.Var) |
| 74 | + if !ok { |
| 75 | + return |
| 76 | + } |
| 77 | + if !isTestingType(v.Type()) { |
| 78 | + return |
| 79 | + } |
| 80 | + // Flag if the variable is defined outside the closure (captured). |
| 81 | + if v.Pos() < funcLit.Pos() || v.Pos() >= funcLit.End() { |
| 82 | + c.pass.Report(analysis.Diagnostic{ |
| 83 | + Pos: ident.Pos(), |
| 84 | + End: ident.End(), |
| 85 | + Message: fmt.Sprintf("cleanup closure captures %s; the test will have ended when cleanup runs", ident.Name), |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func isTestingType(t types.Type) bool { |
| 91 | + if ptr, ok := t.(*types.Pointer); ok { |
| 92 | + t = ptr.Elem() |
| 93 | + } |
| 94 | + named, ok := t.(*types.Named) |
| 95 | + if !ok { |
| 96 | + return false |
| 97 | + } |
| 98 | + obj := named.Obj() |
| 99 | + if obj.Pkg() == nil || obj.Pkg().Path() != "testing" { |
| 100 | + return false |
| 101 | + } |
| 102 | + switch obj.Name() { |
| 103 | + case "T", "B", "F", "TB": |
| 104 | + return true |
| 105 | + } |
| 106 | + return false |
| 107 | +} |
0 commit comments