|
| 1 | +// Package timenowsub implements a Go analysis linter that flags |
| 2 | +// time.Now().Sub(t) calls that can be simplified to time.Since(t). |
| 3 | +package timenowsub |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "go/ast" |
| 8 | + "go/types" |
| 9 | + |
| 10 | + "golang.org/x/tools/go/analysis" |
| 11 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 12 | + |
| 13 | + "github.com/github/gh-aw/pkg/linters/internal/astutil" |
| 14 | + "github.com/github/gh-aw/pkg/linters/internal/filecheck" |
| 15 | + "github.com/github/gh-aw/pkg/linters/internal/nolint" |
| 16 | +) |
| 17 | + |
| 18 | +// Analyzer is the time-now-sub analysis pass. |
| 19 | +var Analyzer = &analysis.Analyzer{ |
| 20 | + Name: "timenowsub", |
| 21 | + Doc: "reports time.Now().Sub(t) calls that should be simplified to time.Since(t)", |
| 22 | + URL: "https://github.com/github/gh-aw/tree/main/pkg/linters/timenowsub", |
| 23 | + Requires: []*analysis.Analyzer{inspect.Analyzer, nolint.Analyzer, filecheck.Analyzer}, |
| 24 | + Run: run, |
| 25 | +} |
| 26 | + |
| 27 | +func run(pass *analysis.Pass) (any, error) { |
| 28 | + insp, err := astutil.Inspector(pass) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + noLintIndex, err := nolint.Index(pass) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + generatedFiles, err := filecheck.Index(pass) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + nodeFilter := []ast.Node{ |
| 42 | + (*ast.CallExpr)(nil), |
| 43 | + } |
| 44 | + |
| 45 | + insp.Preorder(nodeFilter, func(n ast.Node) { |
| 46 | + outer, ok := n.(*ast.CallExpr) |
| 47 | + if !ok { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Match <expr>.Sub(<arg>) where <expr> is time.Now(). |
| 52 | + sel, ok := outer.Fun.(*ast.SelectorExpr) |
| 53 | + if !ok || sel.Sel.Name != "Sub" { |
| 54 | + return |
| 55 | + } |
| 56 | + if len(outer.Args) != 1 { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + // Verify the receiver is a call to time.Now(). |
| 61 | + nowCall, ok := sel.X.(*ast.CallExpr) |
| 62 | + if !ok { |
| 63 | + return |
| 64 | + } |
| 65 | + qualifier, ok := timeNowQualifier(pass, nowCall) |
| 66 | + if !ok { |
| 67 | + return |
| 68 | + } |
| 69 | + if !isSafeSinceArg(outer.Args[0]) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + pos := pass.Fset.PositionFor(outer.Pos(), false) |
| 74 | + if filecheck.ShouldSkipFilename(pos.Filename, generatedFiles) { |
| 75 | + return |
| 76 | + } |
| 77 | + if nolint.HasDirectiveForLinter(pos, noLintIndex, "timenowsub") { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + argText := astutil.NodeText(pass.Fset, outer.Args[0]) |
| 82 | + if argText == "" { |
| 83 | + return |
| 84 | + } |
| 85 | + sinceText := qualifier + ".Since(" + argText + ")" |
| 86 | + |
| 87 | + pass.Report(analysis.Diagnostic{ |
| 88 | + Pos: outer.Pos(), |
| 89 | + End: outer.End(), |
| 90 | + Message: fmt.Sprintf("%s.Now().Sub(%s) can be simplified to %s", qualifier, argText, sinceText), |
| 91 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 92 | + Message: fmt.Sprintf("Replace %s.Now().Sub(%s) with %s", qualifier, argText, sinceText), |
| 93 | + TextEdits: []analysis.TextEdit{{ |
| 94 | + Pos: outer.Pos(), |
| 95 | + End: outer.End(), |
| 96 | + NewText: []byte(sinceText), |
| 97 | + }}, |
| 98 | + }}, |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + return nil, nil |
| 103 | +} |
| 104 | + |
| 105 | +// timeNowQualifier reports the imported identifier used for time.Now(). |
| 106 | +func timeNowQualifier(pass *analysis.Pass, call *ast.CallExpr) (string, bool) { |
| 107 | + if len(call.Args) != 0 { |
| 108 | + return "", false |
| 109 | + } |
| 110 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 111 | + if !ok || sel.Sel.Name != "Now" { |
| 112 | + return "", false |
| 113 | + } |
| 114 | + ident, ok := sel.X.(*ast.Ident) |
| 115 | + if !ok { |
| 116 | + return "", false |
| 117 | + } |
| 118 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 119 | + if obj == nil { |
| 120 | + return "", false |
| 121 | + } |
| 122 | + pkgName, ok := obj.(*types.PkgName) |
| 123 | + if !ok { |
| 124 | + return "", false |
| 125 | + } |
| 126 | + return ident.Name, pkgName.Imported().Path() == "time" |
| 127 | +} |
| 128 | + |
| 129 | +// isSafeSinceArg reports whether expr can be evaluated before time.Now() |
| 130 | +// without introducing calls or other potentially observable behavior changes. |
| 131 | +func isSafeSinceArg(expr ast.Expr) bool { |
| 132 | + switch e := expr.(type) { |
| 133 | + case *ast.Ident: |
| 134 | + return true |
| 135 | + case *ast.ParenExpr: |
| 136 | + return isSafeSinceArg(e.X) |
| 137 | + default: |
| 138 | + return false |
| 139 | + } |
| 140 | +} |
0 commit comments