s1024.go raw
1 package s1024
2
3 import (
4 "go/ast"
5
6 "honnef.co/go/tools/analysis/code"
7 "honnef.co/go/tools/analysis/edit"
8 "honnef.co/go/tools/analysis/facts/generated"
9 "honnef.co/go/tools/analysis/lint"
10 "honnef.co/go/tools/analysis/report"
11 "honnef.co/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: "S1024",
20 Run: run,
21 Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer},
22 },
23 Doc: &lint.RawDocumentation{
24 Title: `Replace \'x.Sub(time.Now())\' with \'time.Until(x)\'`,
25 Text: `The \'time.Until\' helper has the same effect as using \'x.Sub(time.Now())\'
26 but is easier to read.`,
27 Before: `x.Sub(time.Now())`,
28 After: `time.Until(x)`,
29 Since: "2017.1",
30 MergeIf: lint.MergeIfAny,
31 },
32 })
33
34 var Analyzer = SCAnalyzer.Analyzer
35
36 var (
37 checkTimeUntilQ = pattern.MustParse(`(CallExpr (Symbol "(time.Time).Sub") [(CallExpr (Symbol "time.Now") [])])`)
38 checkTimeUntilR = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "time") (Ident "Until")) [arg])`)
39 )
40
41 func run(pass *analysis.Pass) (interface{}, error) {
42 fn := func(node ast.Node) {
43 if _, ok := code.Match(pass, checkTimeUntilQ, node); ok {
44 if sel, ok := node.(*ast.CallExpr).Fun.(*ast.SelectorExpr); ok {
45 r := pattern.NodeToAST(checkTimeUntilR.Root, map[string]interface{}{"arg": sel.X}).(ast.Node)
46 report.Report(pass, node, "should use time.Until instead of t.Sub(time.Now())",
47 report.FilterGenerated(),
48 report.MinimumStdlibVersion("go1.8"),
49 report.Fixes(edit.Fix("replace with call to time.Until", edit.ReplaceWithNode(pass.Fset, node, r))))
50 } else {
51 report.Report(pass, node, "should use time.Until instead of t.Sub(time.Now())",
52 report.MinimumStdlibVersion("go1.8"),
53 report.FilterGenerated())
54 }
55 }
56 }
57 code.Preorder(pass, fn, (*ast.CallExpr)(nil))
58 return nil, nil
59 }
60