|
| 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 waitgroup defines an Analyzer that detects simple misuses |
| 6 | +// of sync.WaitGroup. |
| 7 | +package waitgroup |
| 8 | + |
| 9 | +import ( |
| 10 | + _ "embed" |
| 11 | + "go/ast" |
| 12 | + "go/types" |
| 13 | + "reflect" |
| 14 | + |
| 15 | + "golang.org/x/tools/go/analysis" |
| 16 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 17 | + "golang.org/x/tools/go/analysis/passes/internal/analysisutil" |
| 18 | + "golang.org/x/tools/go/ast/inspector" |
| 19 | + "golang.org/x/tools/go/types/typeutil" |
| 20 | + "golang.org/x/tools/internal/typesinternal" |
| 21 | +) |
| 22 | + |
| 23 | +//go:embed doc.go |
| 24 | +var doc string |
| 25 | + |
| 26 | +var Analyzer = &analysis.Analyzer{ |
| 27 | + Name: "waitgroup", |
| 28 | + Doc: analysisutil.MustExtractDoc(doc, "waitgroup"), |
| 29 | + URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/waitgroup", |
| 30 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 31 | + Run: run, |
| 32 | +} |
| 33 | + |
| 34 | +func run(pass *analysis.Pass) (any, error) { |
| 35 | + if !analysisutil.Imports(pass.Pkg, "sync") { |
| 36 | + return nil, nil // doesn't directly import sync |
| 37 | + } |
| 38 | + |
| 39 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 40 | + nodeFilter := []ast.Node{ |
| 41 | + (*ast.CallExpr)(nil), |
| 42 | + } |
| 43 | + |
| 44 | + inspect.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) (proceed bool) { |
| 45 | + if push { |
| 46 | + call := n.(*ast.CallExpr) |
| 47 | + if fn, ok := typeutil.Callee(pass.TypesInfo, call).(*types.Func); ok && |
| 48 | + isMethodNamed(fn, "sync", "WaitGroup", "Add") && |
| 49 | + hasSuffix(stack, wantSuffix) && |
| 50 | + backindex(stack, 1) == backindex(stack, 2).(*ast.BlockStmt).List[0] { // ExprStmt must be Block's first stmt |
| 51 | + |
| 52 | + pass.Reportf(call.Lparen, "WaitGroup.Add called from inside new goroutine") |
| 53 | + } |
| 54 | + } |
| 55 | + return true |
| 56 | + }) |
| 57 | + |
| 58 | + return nil, nil |
| 59 | +} |
| 60 | + |
| 61 | +// go func() { |
| 62 | +// wg.Add(1) |
| 63 | +// ... |
| 64 | +// }() |
| 65 | +var wantSuffix = []ast.Node{ |
| 66 | + (*ast.GoStmt)(nil), |
| 67 | + (*ast.CallExpr)(nil), |
| 68 | + (*ast.FuncLit)(nil), |
| 69 | + (*ast.BlockStmt)(nil), |
| 70 | + (*ast.ExprStmt)(nil), |
| 71 | + (*ast.CallExpr)(nil), |
| 72 | +} |
| 73 | + |
| 74 | +// hasSuffix reports whether stack has the matching suffix, |
| 75 | +// considering only node types. |
| 76 | +func hasSuffix(stack, suffix []ast.Node) bool { |
| 77 | + // TODO(adonovan): the inspector could implement this for us. |
| 78 | + if len(stack) < len(suffix) { |
| 79 | + return false |
| 80 | + } |
| 81 | + for i := range len(suffix) { |
| 82 | + if reflect.TypeOf(backindex(stack, i)) != reflect.TypeOf(backindex(suffix, i)) { |
| 83 | + return false |
| 84 | + } |
| 85 | + } |
| 86 | + return true |
| 87 | +} |
| 88 | + |
| 89 | +// isMethodNamed reports whether f is a method with the specified |
| 90 | +// package, receiver type, and method names. |
| 91 | +func isMethodNamed(fn *types.Func, pkg, recv, name string) bool { |
| 92 | + if fn.Pkg() != nil && fn.Pkg().Path() == pkg && fn.Name() == name { |
| 93 | + if r := fn.Type().(*types.Signature).Recv(); r != nil { |
| 94 | + if _, gotRecv := typesinternal.ReceiverNamed(r); gotRecv != nil { |
| 95 | + return gotRecv.Obj().Name() == recv |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return false |
| 100 | +} |
| 101 | + |
| 102 | +// backindex is like [slices.Index] but from the back of the slice. |
| 103 | +func backindex[T any](slice []T, i int) T { |
| 104 | + return slice[len(slice)-1-i] |
| 105 | +} |
0 commit comments