| 1 |
package sa2000 |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
|
| 7 |
"honnef.co/go/tools/analysis/code" |
| 8 |
"honnef.co/go/tools/analysis/lint" |
| 9 |
"honnef.co/go/tools/analysis/report" |
| 10 |
"honnef.co/go/tools/pattern" |
| 11 |
|
| 12 |
"golang.org/x/tools/go/analysis" |
| 13 |
) |
| 14 |
|
| 15 |
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ |
| 16 |
Analyzer: &analysis.Analyzer{ |
| 17 |
Name: "SA2000", |
| 18 |
Run: run, |
| 19 |
Requires: code.RequiredAnalyzers, |
| 20 |
}, |
| 21 |
Doc: &lint.RawDocumentation{ |
| 22 |
Title: `\'sync.WaitGroup.Add\' called inside the goroutine, leading to a race condition`, |
| 23 |
Since: "2017.1", |
| 24 |
Severity: lint.SeverityWarning, |
| 25 |
MergeIf: lint.MergeIfAny, |
| 26 |
}, |
| 27 |
}) |
| 28 |
|
| 29 |
var Analyzer = SCAnalyzer.Analyzer |
| 30 |
|
| 31 |
var checkWaitgroupAddQ = pattern.MustParse(` |
| 32 |
(GoStmt |
| 33 |
(CallExpr |
| 34 |
(FuncLit |
| 35 |
_ |
| 36 |
call@(CallExpr (Symbol "(*sync.WaitGroup).Add") _):_) _))`) |
| 37 |
|
| 38 |
func run(pass *analysis.Pass) (any, error) { |
| 39 |
for _, m := range code.Matches(pass, checkWaitgroupAddQ) { |
| 40 |
call := m.State["call"].(ast.Node) |
| 41 |
report.Report(pass, call, fmt.Sprintf("should call %s before starting the goroutine to avoid a race", report.Render(pass, call))) |
| 42 |
} |
| 43 |
return nil, nil |
| 44 |
} |