qf1009.go raw
1 package qf1009
2
3 import (
4 "go/ast"
5 "go/token"
6
7 "honnef.co/go/tools/analysis/code"
8 "honnef.co/go/tools/analysis/edit"
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: "QF1009",
20 Run: run,
21 Requires: []*analysis.Analyzer{inspect.Analyzer},
22 },
23 Doc: &lint.RawDocumentation{
24 Title: `Use \'time.Time.Equal\' instead of \'==\' operator`,
25 Since: "2021.1",
26 Severity: lint.SeverityInfo,
27 },
28 })
29
30 var Analyzer = SCAnalyzer.Analyzer
31
32 var timeEqualR = pattern.MustParse(`(CallExpr (SelectorExpr lhs (Ident "Equal")) rhs)`)
33
34 func run(pass *analysis.Pass) (interface{}, error) {
35 // FIXME(dh): create proper suggested fix for renamed import
36
37 fn := func(node ast.Node) {
38 expr := node.(*ast.BinaryExpr)
39 if expr.Op != token.EQL {
40 return
41 }
42 if !code.IsOfTypeWithName(pass, expr.X, "time.Time") || !code.IsOfTypeWithName(pass, expr.Y, "time.Time") {
43 return
44 }
45 report.Report(pass, node, "probably want to use time.Time.Equal instead",
46 report.Fixes(edit.Fix("Use time.Time.Equal method",
47 edit.ReplaceWithPattern(pass.Fset, node, timeEqualR, pattern.State{"lhs": expr.X, "rhs": expr.Y}))))
48 }
49 code.Preorder(pass, fn, (*ast.BinaryExpr)(nil))
50 return nil, nil
51 }
52