-
Notifications
You must be signed in to change notification settings - Fork 4
/
checker.go
184 lines (167 loc) · 3.74 KB
/
checker.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package wraperr
import (
"go/ast"
"go/token"
"go/types"
"strings"
)
type Checker interface {
Check(ReportFunc)
}
type ReportFunc func(assignedAt, returnedAt token.Pos)
func NewChecker(
fset *token.FileSet,
info *types.Info,
f *ast.FuncDecl,
) Checker {
return &checkerImpl{
fset: fset,
info: info,
funcType: f.Type,
funcBody: f.Body,
}
}
func newClousureChecker(
fset *token.FileSet,
info *types.Info,
f *ast.FuncLit,
) Checker {
return &checkerImpl{
fset: fset,
info: info,
funcType: f.Type,
funcBody: f.Body,
}
}
type checkerImpl struct {
fset *token.FileSet
info *types.Info
funcType *ast.FuncType
funcBody *ast.BlockStmt
errInReturn []bool
errNames map[string]struct{}
errIdents map[string]*errIdent
}
func (c *checkerImpl) Check(report ReportFunc) {
if !c.init() {
return
}
ast.Inspect(c.funcBody, func(n ast.Node) bool {
switch stmt := n.(type) {
case *ast.AssignStmt:
c.checkAssignment(stmt)
case *ast.ReturnStmt:
c.checkReturn(stmt, report)
case *ast.FuncLit:
newClousureChecker(c.fset, c.info, stmt).Check(report)
return false
}
return true
})
}
func (c *checkerImpl) init() (ok bool) {
if c.funcType == nil || c.funcType.Results == nil {
return
}
fields := c.funcType.Results.List
c.errInReturn = make([]bool, 0, 2*len(fields))
c.errNames = make(map[string]struct{}, 2*len(fields))
c.errIdents = map[string]*errIdent{}
for _, f := range fields {
isErr := isErrorType(c.info.TypeOf(f.Type))
ok = ok || isErr
if len(f.Names) == 0 {
c.errInReturn = append(c.errInReturn, isErr)
}
for _, n := range f.Names {
c.errInReturn = append(c.errInReturn, isErr)
c.errNames[n.Name] = struct{}{}
}
}
return
}
func (c *checkerImpl) checkAssignment(stmt *ast.AssignStmt) {
var errIds []*ast.Ident
for _, expr := range stmt.Lhs {
if !isErrorType(c.info.TypeOf(expr)) {
continue
}
if id, ok := expr.(*ast.Ident); ok {
errIds = append(errIds, id)
}
}
if len(errIds) > 0 {
// Detect wrapped error assignment
var wrapped bool
for _, expr := range stmt.Rhs {
if cexpr, ok := expr.(*ast.CallExpr); ok {
if c.isWrapped(cexpr) {
wrapped = true
}
}
}
for _, id := range errIds {
c.errIdents[id.Name] = &errIdent{Ident: id, wrapped: wrapped}
}
}
}
func (c *checkerImpl) checkReturn(stmt *ast.ReturnStmt, report ReportFunc) {
switch len(stmt.Results) {
case 0:
// Named return values
for n := range c.errNames {
if errIdent, ok := c.errIdents[n]; ok && !errIdent.wrapped {
report(errIdent.Pos(), stmt.Return)
}
}
case len(c.errInReturn):
// Simple return
for i, expr := range stmt.Results {
if !c.errInReturn[i] {
continue
}
switch expr := expr.(type) {
case *ast.Ident:
if errIdent, ok := c.errIdents[expr.Name]; ok && !errIdent.wrapped {
report(errIdent.Pos(), expr.NamePos)
}
case *ast.CallExpr:
if !c.isWrapped(expr) {
report(expr.Pos(), expr.Lparen)
}
default:
// TODO: should report unexpected exper
}
}
case 1:
// Return another function directly
switch expr := stmt.Results[0].(type) {
case *ast.CallExpr:
if !c.isWrapped(expr) {
report(expr.Pos(), expr.Pos())
}
default:
// TODO: should report unexpected exper
}
default:
// TODO: should report unexpected exper
}
}
func (c *checkerImpl) isWrapped(call *ast.CallExpr) bool {
switch fexpr := call.Fun.(type) {
case *ast.SelectorExpr:
if f, ok := c.info.ObjectOf(fexpr.Sel).(*types.Func); ok {
chunks := []string{}
for _, chunk := range strings.Split(f.FullName(), "/") {
if chunk == "vendor" {
chunks = []string{}
} else {
chunks = append(chunks, chunk)
}
}
_, ok = wrapperFuncSet[strings.Join(chunks, "/")]
return ok
}
}
return false
}