github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa2000/sa2000.go (about)

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