@@ -15,39 +15,33 @@ type FunctionResultsLimitRule struct {
15
15
configureOnce sync.Once
16
16
}
17
17
18
- const defaultResultsLimit = 3
19
-
20
- func (r * FunctionResultsLimitRule ) configure (arguments lint.Arguments ) {
21
- if len (arguments ) < 1 {
22
- r .max = defaultResultsLimit
23
- return
24
- }
25
-
26
- maxResults , ok := arguments [0 ].(int64 ) // Alt. non panicking version
27
- if ! ok {
28
- panic (fmt .Sprintf (`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T` , arguments [0 ]))
29
- }
30
- if maxResults < 0 {
31
- panic (`the value passed as return results number to the "function-result-limit" rule cannot be negative` )
32
- }
33
-
34
- r .max = int (maxResults )
35
- }
36
-
37
18
// Apply applies the rule to given file.
38
19
func (r * FunctionResultsLimitRule ) Apply (file * lint.File , arguments lint.Arguments ) []lint.Failure {
39
20
r .configureOnce .Do (func () { r .configure (arguments ) })
40
21
41
22
var failures []lint.Failure
23
+ for _ , decl := range file .AST .Decls {
24
+ funcDecl , ok := decl .(* ast.FuncDecl )
25
+ if ! ok {
26
+ continue
27
+ }
42
28
43
- walker := lintFunctionResultsNum {
44
- max : r .max ,
45
- onFailure : func (failure lint.Failure ) {
46
- failures = append (failures , failure )
47
- },
48
- }
29
+ num := 0
30
+ hasResults := funcDecl .Type .Results != nil
31
+ if hasResults {
32
+ num = funcDecl .Type .Results .NumFields ()
33
+ }
34
+
35
+ if num <= r .max {
36
+ continue
37
+ }
49
38
50
- ast .Walk (walker , file .AST )
39
+ failures = append (failures , lint.Failure {
40
+ Confidence : 1 ,
41
+ Failure : fmt .Sprintf ("maximum number of return results per function exceeded; max %d but got %d" , r .max , num ),
42
+ Node : funcDecl .Type ,
43
+ })
44
+ }
51
45
52
46
return failures
53
47
}
@@ -57,29 +51,21 @@ func (*FunctionResultsLimitRule) Name() string {
57
51
return "function-result-limit"
58
52
}
59
53
60
- type lintFunctionResultsNum struct {
61
- max int
62
- onFailure func (lint.Failure )
63
- }
54
+ const defaultResultsLimit = 3
64
55
65
- func (w lintFunctionResultsNum ) Visit (n ast.Node ) ast.Visitor {
66
- node , ok := n .(* ast.FuncDecl )
67
- if ok {
68
- num := 0
69
- hasResults := node .Type .Results != nil
70
- if hasResults {
71
- num = node .Type .Results .NumFields ()
72
- }
73
- if num > w .max {
74
- w .onFailure (lint.Failure {
75
- Confidence : 1 ,
76
- Failure : fmt .Sprintf ("maximum number of return results per function exceeded; max %d but got %d" , w .max , num ),
77
- Node : node .Type ,
78
- })
79
- }
56
+ func (r * FunctionResultsLimitRule ) configure (arguments lint.Arguments ) {
57
+ if len (arguments ) < 1 {
58
+ r .max = defaultResultsLimit
59
+ return
60
+ }
80
61
81
- return nil // skip visiting function's body
62
+ maxResults , ok := arguments [0 ].(int64 ) // Alt. non panicking version
63
+ if ! ok {
64
+ panic (fmt .Sprintf (`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T` , arguments [0 ]))
65
+ }
66
+ if maxResults < 0 {
67
+ panic (`the value passed as return results number to the "function-result-limit" rule cannot be negative` )
82
68
}
83
69
84
- return w
70
+ r . max = int ( maxResults )
85
71
}
0 commit comments