-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnakedret.go
316 lines (279 loc) · 7.64 KB
/
nakedret.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package nakedret
import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const pwd = "./"
func NakedReturnAnalyzer(defaultLines uint, skipTestFiles bool) *analysis.Analyzer {
nakedRet := &NakedReturnRunner{}
flags := flag.NewFlagSet("nakedret", flag.ExitOnError)
flags.UintVar(&nakedRet.MaxLength, "l", defaultLines, "maximum number of lines for a naked return function")
flags.BoolVar(&nakedRet.SkipTestFiles, "skip-test-files", skipTestFiles, "set to true to skip test files")
var analyzer = &analysis.Analyzer{
Name: "nakedret",
Doc: "Checks that functions with naked returns are not longer than a maximum size (can be zero).",
Run: nakedRet.run,
Flags: *flags,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
return analyzer
}
type NakedReturnRunner struct {
MaxLength uint
SkipTestFiles bool
}
func (n *NakedReturnRunner) run(pass *analysis.Pass) (any, error) {
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{ // filter needed nodes: visit only them
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
(*ast.ReturnStmt)(nil),
}
retVis := &returnsVisitor{
pass: pass,
f: pass.Fset,
maxLength: n.MaxLength,
skipTestFiles: n.SkipTestFiles,
}
inspector.Nodes(nodeFilter, retVis.NodesVisit)
return nil, nil
}
type returnsVisitor struct {
pass *analysis.Pass
f *token.FileSet
maxLength uint
skipTestFiles bool
// functions contains funcInfo for each nested function definition encountered while visiting the AST.
functions []funcInfo
}
type funcInfo struct {
// Details of the function we're currently dealing with
funcType *ast.FuncType
funcName string
funcLength int
reportNaked bool
}
func checkNakedReturns(args []string, maxLength *uint, skipTestFiles bool, setExitStatus bool) error {
fset := token.NewFileSet()
files, err := parseInput(args, fset)
if err != nil {
return fmt.Errorf("could not parse input: %v", err)
}
if maxLength == nil {
return errors.New("max length nil")
}
analyzer := NakedReturnAnalyzer(*maxLength, skipTestFiles)
pass := &analysis.Pass{
Analyzer: analyzer,
Fset: fset,
Files: files,
Report: func(d analysis.Diagnostic) {
log.Printf("%s:%d: %s", fset.Position(d.Pos).Filename, fset.Position(d.Pos).Line, d.Message)
},
ResultOf: map[*analysis.Analyzer]any{},
}
result, err := inspect.Analyzer.Run(pass)
if err != nil {
return err
}
pass.ResultOf[inspect.Analyzer] = result
_, err = analyzer.Run(pass)
if err != nil {
return err
}
return nil
}
func parseInput(args []string, fset *token.FileSet) ([]*ast.File, error) {
var directoryList []string
var fileMode bool
files := make([]*ast.File, 0)
if len(args) == 0 {
directoryList = append(directoryList, pwd)
} else {
for _, arg := range args {
if strings.HasSuffix(arg, "/...") && isDir(arg[:len(arg)-len("/...")]) {
for _, dirname := range allPackagesInFS(arg) {
directoryList = append(directoryList, dirname)
}
} else if isDir(arg) {
directoryList = append(directoryList, arg)
} else if exists(arg) {
if strings.HasSuffix(arg, ".go") {
fileMode = true
f, err := parser.ParseFile(fset, arg, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
} else {
return nil, fmt.Errorf("invalid file %v specified", arg)
}
} else {
// TODO clean this up a bit
imPaths := importPaths([]string{arg})
for _, importPath := range imPaths {
pkg, err := build.Import(importPath, ".", 0)
if err != nil {
return nil, err
}
var stringFiles []string
stringFiles = append(stringFiles, pkg.GoFiles...)
// files = append(files, pkg.CgoFiles...)
stringFiles = append(stringFiles, pkg.TestGoFiles...)
if pkg.Dir != "." {
for i, f := range stringFiles {
stringFiles[i] = filepath.Join(pkg.Dir, f)
}
}
fileMode = true
for _, stringFile := range stringFiles {
f, err := parser.ParseFile(fset, stringFile, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
}
}
}
}
}
// if we're not in file mode, then we need to grab each and every package in each directory
// we can to grab all the files
if !fileMode {
for _, fpath := range directoryList {
pkgs, err := parser.ParseDir(fset, fpath, nil, 0)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
for _, f := range pkg.Files {
files = append(files, f)
}
}
}
}
return files, nil
}
func isDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func hasNamedReturns(funcType *ast.FuncType) bool {
if funcType == nil || funcType.Results == nil {
return false
}
for _, field := range funcType.Results.List {
for _, ident := range field.Names {
if ident != nil {
return true
}
}
}
return false
}
func nestedFuncName(functions []funcInfo) string {
var names []string
for _, f := range functions {
names = append(names, f.funcName)
}
return strings.Join(names, ".")
}
func nakedReturnFix(s *ast.ReturnStmt, funcType *ast.FuncType) *ast.ReturnStmt {
var nameExprs []ast.Expr
for _, result := range funcType.Results.List {
for _, ident := range result.Names {
if ident != nil {
nameExprs = append(nameExprs, ident)
}
}
}
var sFix = *s
sFix.Results = nameExprs
return &sFix
}
func (v *returnsVisitor) NodesVisit(node ast.Node, push bool) bool {
var (
funcType *ast.FuncType
funcName string
)
switch s := node.(type) {
case *ast.FuncDecl:
// We've found a function
funcType = s.Type
funcName = s.Name.Name
case *ast.FuncLit:
// We've found a function literal
funcType = s.Type
file := v.f.File(s.Pos())
funcName = fmt.Sprintf("<func():%v>", file.Position(s.Pos()).Line)
case *ast.ReturnStmt:
// We've found a possibly naked return statement
fun := v.functions[len(v.functions)-1]
funName := nestedFuncName(v.functions)
if fun.reportNaked && len(s.Results) == 0 && push {
sFix := nakedReturnFix(s, fun.funcType)
b := &bytes.Buffer{}
err := printer.Fprint(b, v.f, sFix)
if err != nil {
log.Printf("failed to format named return fix: %s", err)
}
v.pass.Report(analysis.Diagnostic{
Pos: s.Pos(),
End: s.End(),
Message: fmt.Sprintf("naked return in func `%s` with %d lines of code", funName, fun.funcLength),
SuggestedFixes: []analysis.SuggestedFix{{
Message: "explicit return statement",
TextEdits: []analysis.TextEdit{{
Pos: s.Pos(),
End: s.End(),
NewText: b.Bytes()}},
}},
})
}
}
if !push {
if funcType == nil {
return false
}
// Pop function info
v.functions = v.functions[:len(v.functions)-1]
return false
}
if push && funcType != nil {
// Push function info to track returns for this function
file := v.f.File(node.Pos())
if v.skipTestFiles && strings.HasSuffix(file.Name(), "_test.go") {
return false
}
length := file.Position(node.End()).Line - file.Position(node.Pos()).Line
if length == 0 {
// consider functions that finish on the same line as they start as single line functions, not zero lines!
length = 1
}
v.functions = append(v.functions, funcInfo{
funcType: funcType,
funcName: funcName,
funcLength: length,
reportNaked: uint(length) > v.maxLength && hasNamedReturns(funcType),
})
}
return true
}