forked from mgechev/revive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunhandled_error.go
175 lines (144 loc) · 3.9 KB
/
unhandled_error.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
package rule
import (
"errors"
"fmt"
"go/ast"
"go/types"
"regexp"
"strings"
"github.com/mgechev/revive/lint"
)
// UnhandledErrorRule warns on unhandled errors returned by function calls.
type UnhandledErrorRule struct {
ignoreList []*regexp.Regexp
}
// Configure validates the rule configuration, and configures the rule accordingly.
//
// Configuration implements the [lint.ConfigurableRule] interface.
func (r *UnhandledErrorRule) Configure(arguments lint.Arguments) error {
for _, arg := range arguments {
argStr, ok := arg.(string)
if !ok {
return fmt.Errorf("invalid argument to the unhandled-error rule. Expecting a string, got %T", arg)
}
argStr = strings.Trim(argStr, " ")
if argStr == "" {
return errors.New("invalid argument to the unhandled-error rule, expected regular expression must not be empty")
}
exp, err := regexp.Compile(argStr)
if err != nil {
return fmt.Errorf("invalid argument to the unhandled-error rule: regexp %q does not compile: %w", argStr, err)
}
r.ignoreList = append(r.ignoreList, exp)
}
return nil
}
// Apply applies the rule to given file.
func (r *UnhandledErrorRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
walker := &lintUnhandledErrors{
ignoreList: r.ignoreList,
pkg: file.Pkg,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
file.Pkg.TypeCheck()
ast.Walk(walker, file.AST)
return failures
}
// Name returns the rule name.
func (*UnhandledErrorRule) Name() string {
return "unhandled-error"
}
type lintUnhandledErrors struct {
ignoreList []*regexp.Regexp
pkg *lint.Package
onFailure func(lint.Failure)
}
// Visit looks for statements that are function calls.
// If the called function returns a value of type error a failure will be created.
func (w *lintUnhandledErrors) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.ExprStmt:
fCall, ok := n.X.(*ast.CallExpr)
if !ok {
return nil // not a function call
}
funcType := w.pkg.TypeOf(fCall)
if funcType == nil {
return nil // skip, type info not available
}
switch t := funcType.(type) {
case *types.Named:
if !w.isTypeError(t) {
return nil // func call does not return an error
}
w.addFailure(fCall)
default:
retTypes, ok := funcType.Underlying().(*types.Tuple)
if !ok {
return nil // skip, unable to retrieve return type of the called function
}
if w.returnsAnError(retTypes) {
w.addFailure(fCall)
}
}
}
return w
}
func (w *lintUnhandledErrors) addFailure(n *ast.CallExpr) {
name := w.funcName(n)
if w.isIgnoredFunc(name) {
return
}
w.onFailure(lint.Failure{
Category: "bad practice",
Confidence: 1,
Node: n,
Failure: fmt.Sprintf("Unhandled error in call to function %v", name),
})
}
func (w *lintUnhandledErrors) funcName(call *ast.CallExpr) string {
fn, ok := w.getFunc(call)
if !ok {
return gofmt(call.Fun)
}
name := fn.FullName()
name = strings.ReplaceAll(name, "(", "")
name = strings.ReplaceAll(name, ")", "")
name = strings.ReplaceAll(name, "*", "")
return name
}
func (w *lintUnhandledErrors) isIgnoredFunc(funcName string) bool {
for _, pattern := range w.ignoreList {
if len(pattern.FindString(funcName)) == len(funcName) {
return true
}
}
return false
}
func (*lintUnhandledErrors) isTypeError(t *types.Named) bool {
const errorTypeName = "_.error"
return t.Obj().Id() == errorTypeName
}
func (w *lintUnhandledErrors) returnsAnError(tt *types.Tuple) bool {
for i := 0; i < tt.Len(); i++ {
nt, ok := tt.At(i).Type().(*types.Named)
if ok && w.isTypeError(nt) {
return true
}
}
return false
}
func (w *lintUnhandledErrors) getFunc(call *ast.CallExpr) (*types.Func, bool) {
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return nil, false
}
fn, ok := w.pkg.TypesInfo().ObjectOf(sel.Sel).(*types.Func)
if !ok {
return nil, false
}
return fn, true
}