-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlerrors.go
156 lines (141 loc) · 3.46 KB
/
handlerrors.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
package handlerrors
import (
"fmt"
"go/ast"
"go/types"
"strings"
"github.com/gostaticanalysis/comment/passes/commentmap"
"github.com/k1LoW/gostyle/config"
"github.com/k1LoW/gostyle/reporter"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const (
name = "handlerrors"
doc = "Analyzer based on https://go.dev/wiki/CodeReviewComments#handle-errors"
msg = "Do not discard errors using `_` variables. If a function returns an error, check it to make sure the function succeeded. Handle the error, return it, or, in truly exceptional situations, panic. (ref: https://go.dev/wiki/CodeReviewComments#handle-errors )"
)
var (
disable bool
includeGenerated bool
excludeTest bool
)
// Analyzer based on https://go.dev/wiki/CodeReviewComments#handle-errors
var Analyzer = &analysis.Analyzer{
Name: name,
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
commentmap.Analyzer,
},
}
// AnalyzerWithConfig based on https://go.dev/wiki/CodeReviewComments#handle-errors
var AnalyzerWithConfig = &analysis.Analyzer{
Name: name,
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
config.Loader,
inspect.Analyzer,
commentmap.Analyzer,
},
}
var errTyp = func() *types.Interface {
return types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
}()
func run(pass *analysis.Pass) (any, error) {
c, err := config.Load(pass)
if err != nil {
return nil, err
}
if c != nil {
disable = c.IsDisabled(name)
includeGenerated = c.AnalyzersSettings.Handlerrors.IncludeGenerated
excludeTest = c.AnalyzersSettings.Handlerrors.ExcludeTest
}
if disable {
return nil, nil
}
i, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, fmt.Errorf("unexpected result type from inspect: %T", pass.ResultOf[inspect.Analyzer])
}
nodeFilter := []ast.Node{
(*ast.AssignStmt)(nil),
}
var opts []reporter.Option
if includeGenerated {
opts = append(opts, reporter.IncludeGenerated())
}
r, err := reporter.New(name, pass, opts...)
if err != nil {
return nil, err
}
br := &blankErrReporter{
r: r,
pass: pass,
}
i.Preorder(nodeFilter, func(n ast.Node) {
if excludeTest {
if strings.HasSuffix(pass.Fset.File(n.Pos()).Name(), "_test.go") {
return
}
}
switch nn := n.(type) {
case *ast.AssignStmt:
if len(nn.Rhs) == 0 {
return
}
e, ok := nn.Rhs[0].(*ast.CallExpr)
if !ok {
return
}
for i, l := range nn.Lhs {
id, ok := l.(*ast.Ident)
if !ok {
continue
}
if id.Name != "_" {
continue
}
br.report(e, i)
}
}
})
r.Report()
return nil, nil
}
func init() {
Analyzer.Flags.BoolVar(&disable, "disable", false, "disable "+name+" analyzer")
Analyzer.Flags.BoolVar(&includeGenerated, "include-generated", false, "include generated codes")
Analyzer.Flags.BoolVar(&excludeTest, "exclude-test", false, "exclude test files")
}
type blankErrReporter struct {
r *reporter.Reporter
pass *analysis.Pass
}
func (br *blankErrReporter) report(e *ast.CallExpr, i int) {
typ, ok := br.pass.TypesInfo.Types[e]
if !ok {
return
}
switch t := typ.Type.(type) {
case *types.Named:
if types.Implements(t, errTyp) {
br.r.Append(e.Pos(), msg)
}
case *types.Pointer:
if types.Implements(t, errTyp) {
br.r.Append(e.Pos(), msg)
}
case *types.Tuple:
if t.Len() <= i {
return
}
if types.Implements(t.At(i).Type(), errTyp) {
br.r.Append(e.Pos(), msg)
}
}
}