-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
analyzer.go
135 lines (108 loc) · 2.65 KB
/
analyzer.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
package analyzer
import (
"flag"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const FlagAllowErrorInDefer = "allow-error-in-defer"
var Analyzer = &analysis.Analyzer{
Name: "nonamedreturns",
Doc: "Reports all named returns",
Flags: flags(),
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func flags() flag.FlagSet {
fs := flag.FlagSet{}
fs.Bool(FlagAllowErrorInDefer, false, "do not complain about named error, if it is assigned inside defer")
return fs
}
func run(pass *analysis.Pass) (interface{}, error) {
allowErrorInDefer := pass.Analyzer.Flags.Lookup(FlagAllowErrorInDefer).Value.String() == "true"
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
// only filter function defintions
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
}
inspector.Preorder(nodeFilter, func(node ast.Node) {
var funcResults *ast.FieldList
var funcBody *ast.BlockStmt
switch n := node.(type) {
case *ast.FuncLit:
funcResults = n.Type.Results
funcBody = n.Body
case *ast.FuncDecl:
funcResults = n.Type.Results
funcBody = n.Body
default:
return
}
// no return values
if funcResults == nil {
return
}
resultsList := funcResults.List
for _, p := range resultsList {
if len(p.Names) == 0 {
// all good, the parameter is not named
continue
}
for _, n := range p.Names {
if n.Name == "_" {
continue
}
if allowErrorInDefer {
if ident, ok := p.Type.(*ast.Ident); ok {
if ident.Name == "error" && findDeferWithErrorAssignment(funcBody, n.Name) {
continue
}
}
}
pass.Reportf(node.Pos(), "named return %q with type %q found", n.Name, types.ExprString(p.Type))
}
}
})
return nil, nil
}
func findDeferWithErrorAssignment(body *ast.BlockStmt, name string) bool {
found := false
ast.Inspect(body, func(node ast.Node) bool {
if found {
return false // stop inspection
}
if d, ok := node.(*ast.DeferStmt); ok {
if fn, ok2 := d.Call.Fun.(*ast.FuncLit); ok2 {
if findErrorAssignment(fn.Body, name) {
found = true
return false
}
}
}
return true
})
return found
}
func findErrorAssignment(body *ast.BlockStmt, name string) bool {
found := false
ast.Inspect(body, func(node ast.Node) bool {
if found {
return false // stop inspection
}
if a, ok := node.(*ast.AssignStmt); ok {
for _, lh := range a.Lhs {
if i, ok2 := lh.(*ast.Ident); ok2 {
if i.Name == name {
found = true
return false
}
}
}
}
return true
})
return found
}