forked from alecthomas/gometalinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errcheck.go
157 lines (144 loc) · 3.71 KB
/
errcheck.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package errcheck
import (
"go/types"
"honnef.co/go/tools/functions"
"honnef.co/go/tools/lint"
"honnef.co/go/tools/ssa"
)
type Checker struct {
funcDescs *functions.Descriptions
}
func NewChecker() *Checker {
return &Checker{}
}
func (*Checker) Name() string { return "errcheck" }
func (*Checker) Prefix() string { return "ERR" }
func (c *Checker) Funcs() map[string]lint.Func {
return map[string]lint.Func{
"ERR1000": c.CheckErrcheck,
}
}
func (c *Checker) Init(prog *lint.Program) {
c.funcDescs = functions.NewDescriptions(prog.SSA)
}
func (c *Checker) CheckErrcheck(j *lint.Job) {
for _, ssafn := range j.Program.InitialFunctions {
for _, b := range ssafn.Blocks {
for _, ins := range b.Instrs {
ssacall, ok := ins.(ssa.CallInstruction)
if !ok {
continue
}
switch lint.CallName(ssacall.Common()) {
case "fmt.Print", "fmt.Println", "fmt.Printf":
continue
}
isRecover := false
if builtin, ok := ssacall.Common().Value.(*ssa.Builtin); ok {
isRecover = ok && builtin.Name() == "recover"
}
switch ins := ins.(type) {
case ssa.Value:
refs := ins.Referrers()
if refs == nil || len(lint.FilterDebug(*refs)) != 0 {
continue
}
case ssa.Instruction:
// will be a 'go' or 'defer', neither of which has usable return values
default:
// shouldn't happen
continue
}
if ssacall.Common().IsInvoke() {
if sc, ok := ssacall.Common().Value.(*ssa.Call); ok {
// TODO(dh): support multiple levels of
// interfaces, not just one
ssafn := sc.Common().StaticCallee()
if ssafn != nil {
ct := c.funcDescs.Get(ssafn).ConcreteReturnTypes
// TODO(dh): support >1 concrete types
if ct != nil && len(ct) == 1 {
// TODO(dh): do we have access to a
// cached method set somewhere?
ms := types.NewMethodSet(ct[0].At(ct[0].Len() - 1).Type())
// TODO(dh): where can we get the pkg
// for Lookup? Passing nil works fine
// for exported methods, but will fail
// on unexported ones
// TODO(dh): holy nesting and poor
// variable names, clean this up
fn, _ := ms.Lookup(nil, ssacall.Common().Method.Name()).Obj().(*types.Func)
if fn != nil {
ssafn := j.Program.SSA.FuncValue(fn)
if ssafn != nil {
if c.funcDescs.Get(ssafn).NilError {
continue
}
}
}
}
}
}
} else {
ssafn := ssacall.Common().StaticCallee()
if ssafn != nil {
if c.funcDescs.Get(ssafn).NilError {
// Don't complain when the error is known to be nil
continue
}
}
}
switch lint.CallName(ssacall.Common()) {
case "(*os.File).Close":
recv := ssacall.Common().Args[0]
if isReadOnlyFile(recv, nil) {
continue
}
}
res := ssacall.Common().Signature().Results()
if res.Len() == 0 {
continue
}
if !isRecover {
last := res.At(res.Len() - 1)
if types.TypeString(last.Type(), nil) != "error" {
continue
}
}
j.Errorf(ins, "unchecked error")
}
}
}
}
func isReadOnlyFile(val ssa.Value, seen map[ssa.Value]bool) bool {
if seen == nil {
seen = map[ssa.Value]bool{}
}
if seen[val] {
return true
}
seen[val] = true
switch val := val.(type) {
case *ssa.Phi:
for _, edge := range val.Edges {
if !isReadOnlyFile(edge, seen) {
return false
}
}
return true
case *ssa.Extract:
call, ok := val.Tuple.(*ssa.Call)
if !ok {
return false
}
switch lint.CallName(call.Common()) {
case "os.Open":
return true
case "os.OpenFile":
flags, ok := call.Common().Args[1].(*ssa.Const)
return ok && flags.Uint64() == 0
}
return false
}
return false
}