@@ -13,58 +13,48 @@ type FlagParamRule struct{}
13
13
// Apply applies the rule to given file.
14
14
func (* FlagParamRule ) Apply (file * lint.File , _ lint.Arguments ) []lint.Failure {
15
15
var failures []lint.Failure
16
-
17
16
onFailure := func (failure lint.Failure ) {
18
17
failures = append (failures , failure )
19
18
}
20
19
21
- w := lintFlagParamRule {onFailure : onFailure }
22
- ast .Walk (w , file .AST )
23
- return failures
24
- }
25
-
26
- // Name returns the rule name.
27
- func (* FlagParamRule ) Name () string {
28
- return "flag-parameter"
29
- }
30
-
31
- type lintFlagParamRule struct {
32
- onFailure func (lint.Failure )
33
- }
34
-
35
- func (w lintFlagParamRule ) Visit (node ast.Node ) ast.Visitor {
36
- fd , ok := node .(* ast.FuncDecl )
37
- if ! ok {
38
- return w
39
- }
40
-
41
- if fd .Body == nil {
42
- return nil // skip whole function declaration
43
- }
20
+ for _ , decl := range file .AST .Decls {
21
+ fd , ok := decl .(* ast.FuncDecl )
22
+ isFuncWithNonEmptyBody := ok && fd .Body != nil
23
+ if ! isFuncWithNonEmptyBody {
24
+ continue
25
+ }
44
26
45
- for _ , p := range fd .Type .Params .List {
46
- t := p .Type
27
+ boolParams := map [string ]struct {}{}
28
+ for _ , param := range fd .Type .Params .List {
29
+ if ! isIdent (param .Type , "bool" ) {
30
+ continue
31
+ }
47
32
48
- id , ok := t .( * ast. Ident )
49
- if ! ok {
50
- continue
33
+ for _ , paramIdent := range param . Names {
34
+ boolParams [ paramIdent . Name ] = struct {}{}
35
+ }
51
36
}
52
37
53
- if id . Name != "bool" {
38
+ if len ( boolParams ) == 0 {
54
39
continue
55
40
}
56
41
57
- cv := conditionVisitor {p . Names , fd , w }
42
+ cv := conditionVisitor {boolParams , fd , onFailure }
58
43
ast .Walk (cv , fd .Body )
59
44
}
60
45
61
- return w
46
+ return failures
47
+ }
48
+
49
+ // Name returns the rule name.
50
+ func (* FlagParamRule ) Name () string {
51
+ return "flag-parameter"
62
52
}
63
53
64
54
type conditionVisitor struct {
65
- ids [] * ast. Ident
66
- fd * ast.FuncDecl
67
- linter lintFlagParamRule
55
+ idents map [ string ] struct {}
56
+ fd * ast.FuncDecl
57
+ onFailure func (lint. Failure )
68
58
}
69
59
70
60
func (w conditionVisitor ) Visit (node ast.Node ) ast.Visitor {
@@ -73,28 +63,22 @@ func (w conditionVisitor) Visit(node ast.Node) ast.Visitor {
73
63
return w
74
64
}
75
65
76
- fselect := func (n ast.Node ) bool {
66
+ findUsesOfIdents := func (n ast.Node ) bool {
77
67
ident , ok := n .(* ast.Ident )
78
68
if ! ok {
79
69
return false
80
70
}
81
71
82
- for _ , id := range w .ids {
83
- if ident .Name == id .Name {
84
- return true
85
- }
86
- }
87
-
88
- return false
72
+ return w .idents [ident .Name ] == struct {}{}
89
73
}
90
74
91
- uses := pick (ifStmt .Cond , fselect )
75
+ uses := pick (ifStmt .Cond , findUsesOfIdents )
92
76
93
77
if len (uses ) < 1 {
94
78
return w
95
79
}
96
80
97
- w .linter . onFailure (lint.Failure {
81
+ w .onFailure (lint.Failure {
98
82
Confidence : 1 ,
99
83
Node : w .fd .Type .Params ,
100
84
Category : "bad practice" ,
0 commit comments