-
Notifications
You must be signed in to change notification settings - Fork 1
/
inspector.go
101 lines (83 loc) · 1.62 KB
/
inspector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"go/ast"
"go/types"
)
type inspector struct {
tinf types.Info
problems []problem
}
func (insp *inspector) addProblem(p problem) {
insp.problems = append(insp.problems, p)
}
func (insp *inspector) cleanProblems() {
insp.problems = nil
}
func (insp *inspector) inspect(n ast.Node) []problem {
defer insp.cleanProblems()
ast.Inspect(n, insp.inspectFunc)
return insp.problems
}
func (insp *inspector) inspectFunc(node ast.Node) bool {
switch n := node.(type) {
case *ast.CallExpr:
sel, ok := n.Fun.(*ast.SelectorExpr)
if !ok {
break
}
fn, ok := insp.tinf.ObjectOf(sel.Sel).(*types.Func)
if !ok {
break
}
sig, ok := fn.Type().(*types.Signature)
if !ok {
break
}
for i := 0; i < sig.Params().Len(); i++ {
param := sig.Params().At(i)
if param.Type().String() != "time.Duration" {
continue
}
if insp.isSuspicious(n.Args[i]) {
p := problem{
call: n,
}
insp.addProblem(p)
}
}
}
return true
}
func (insp *inspector) isSuspicious(param ast.Expr) bool {
switch p := param.(type) {
case *ast.BasicLit:
//if p.Kind != token.INT {
// return false
//}
if p.Value == "0" {
// 0 is unambiguous
return false
}
return true
case *ast.BinaryExpr:
typ := insp.tinf.TypeOf(p.X)
if typ.String() == "time.Duration" {
return false
}
return true
case *ast.Ident:
obj := insp.tinf.ObjectOf(p)
c, ok := obj.(*types.Const)
if !ok {
return false
}
b, ok := c.Type().(*types.Basic)
if !ok {
return false
}
if b.Kind() == types.UntypedInt {
return true
}
}
return false // assume everything else is ok
}