Skip to content

Commit fcfd6ad

Browse files
authored
refactor (rule/flag-param): replace AST walker by iteration over declarations (#1164)
1 parent 9a5c95f commit fcfd6ad

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

rule/flag_param.go

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,48 @@ type FlagParamRule struct{}
1313
// Apply applies the rule to given file.
1414
func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
1515
var failures []lint.Failure
16-
1716
onFailure := func(failure lint.Failure) {
1817
failures = append(failures, failure)
1918
}
2019

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+
}
4426

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+
}
4732

48-
id, ok := t.(*ast.Ident)
49-
if !ok {
50-
continue
33+
for _, paramIdent := range param.Names {
34+
boolParams[paramIdent.Name] = struct{}{}
35+
}
5136
}
5237

53-
if id.Name != "bool" {
38+
if len(boolParams) == 0 {
5439
continue
5540
}
5641

57-
cv := conditionVisitor{p.Names, fd, w}
42+
cv := conditionVisitor{boolParams, fd, onFailure}
5843
ast.Walk(cv, fd.Body)
5944
}
6045

61-
return w
46+
return failures
47+
}
48+
49+
// Name returns the rule name.
50+
func (*FlagParamRule) Name() string {
51+
return "flag-parameter"
6252
}
6353

6454
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)
6858
}
6959

7060
func (w conditionVisitor) Visit(node ast.Node) ast.Visitor {
@@ -73,28 +63,22 @@ func (w conditionVisitor) Visit(node ast.Node) ast.Visitor {
7363
return w
7464
}
7565

76-
fselect := func(n ast.Node) bool {
66+
findUsesOfIdents := func(n ast.Node) bool {
7767
ident, ok := n.(*ast.Ident)
7868
if !ok {
7969
return false
8070
}
8171

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{}{}
8973
}
9074

91-
uses := pick(ifStmt.Cond, fselect)
75+
uses := pick(ifStmt.Cond, findUsesOfIdents)
9276

9377
if len(uses) < 1 {
9478
return w
9579
}
9680

97-
w.linter.onFailure(lint.Failure{
81+
w.onFailure(lint.Failure{
9882
Confidence: 1,
9983
Node: w.fd.Type.Params,
10084
Category: "bad practice",

0 commit comments

Comments
 (0)