github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1012/s1012.go (about) 1 package s1012 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: "S1012", 20 Run: run, 21 Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer}, 22 }, 23 Doc: &lint.Documentation{ 24 Title: `Replace \'time.Now().Sub(x)\' with \'time.Since(x)\'`, 25 Text: `The \'time.Since\' helper has the same effect as using \'time.Now().Sub(x)\' 26 but is easier to read.`, 27 Before: `time.Now().Sub(x)`, 28 After: `time.Since(x)`, 29 Since: "2017.1", 30 MergeIf: lint.MergeIfAny, 31 }, 32 }) 33 34 var Analyzer = SCAnalyzer.Analyzer 35 36 var ( 37 checkTimeSinceQ = pattern.MustParse(`(CallExpr (SelectorExpr (CallExpr (Symbol "time.Now") []) (Symbol "(time.Time).Sub")) [arg])`) 38 checkTimeSinceR = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "time") (Ident "Since")) [arg])`) 39 ) 40 41 func run(pass *analysis.Pass) (interface{}, error) { 42 fn := func(node ast.Node) { 43 if _, edits, ok := code.MatchAndEdit(pass, checkTimeSinceQ, checkTimeSinceR, node); ok { 44 report.Report(pass, node, "should use time.Since instead of time.Now().Sub", 45 report.FilterGenerated(), 46 report.Fixes(edit.Fix("replace with call to time.Since", edits...))) 47 } 48 } 49 code.Preorder(pass, fn, (*ast.CallExpr)(nil)) 50 return nil, nil 51 }