github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1037/s1037.go (about) 1 package s1037 2 3 import ( 4 "go/ast" 5 6 "github.com/amarpal/go-tools/analysis/code" 7 "github.com/amarpal/go-tools/analysis/edit" 8 "github.com/amarpal/go-tools/analysis/facts/generated" 9 "github.com/amarpal/go-tools/analysis/lint" 10 "github.com/amarpal/go-tools/analysis/report" 11 "github.com/amarpal/go-tools/pattern" 12 13 "golang.org/x/tools/go/analysis" 14 "golang.org/x/tools/go/analysis/passes/inspect" 15 ) 16 17 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ 18 Analyzer: &analysis.Analyzer{ 19 Name: "S1037", 20 Run: run, 21 Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer}, 22 }, 23 Doc: &lint.Documentation{ 24 Title: `Elaborate way of sleeping`, 25 Text: `Using a select statement with a single case receiving 26 from the result of \'time.After\' is a very elaborate way of sleeping that 27 can much simpler be expressed with a simple call to time.Sleep.`, 28 Since: "2020.1", 29 MergeIf: lint.MergeIfAny, 30 }, 31 }) 32 33 var Analyzer = SCAnalyzer.Analyzer 34 35 var ( 36 checkElaborateSleepQ = pattern.MustParse(`(SelectStmt (CommClause (UnaryExpr "<-" (CallExpr (Symbol "time.After") [arg])) body))`) 37 checkElaborateSleepR = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "time") (Ident "Sleep")) [arg])`) 38 ) 39 40 func run(pass *analysis.Pass) (interface{}, error) { 41 fn := func(node ast.Node) { 42 if m, ok := code.Match(pass, checkElaborateSleepQ, node); ok { 43 if body, ok := m.State["body"].([]ast.Stmt); ok && len(body) == 0 { 44 report.Report(pass, node, "should use time.Sleep instead of elaborate way of sleeping", 45 report.ShortRange(), 46 report.FilterGenerated(), 47 report.Fixes(edit.Fix("Use time.Sleep", edit.ReplaceWithPattern(pass.Fset, node, checkElaborateSleepR, m.State)))) 48 } else { 49 // TODO(dh): we could make a suggested fix if the body 50 // doesn't declare or shadow any identifiers 51 report.Report(pass, node, "should use time.Sleep instead of elaborate way of sleeping", 52 report.ShortRange(), 53 report.FilterGenerated()) 54 } 55 } 56 } 57 code.Preorder(pass, fn, (*ast.SelectStmt)(nil)) 58 return nil, nil 59 }