Skip to content

Commit

Permalink
refactor (rule/get-return): replace AST walker by iteration over decl…
Browse files Browse the repository at this point in the history
…arations (#1168)
  • Loading branch information
chavacava authored Dec 7, 2024
1 parent cb89a4c commit 3e1dbc0
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions rule/get_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ type GetReturnRule struct{}
func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
for _, decl := range file.AST.Decls {
fd, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}

if !isGetter(fd.Name.Name) {
continue
}

if hasResults(fd.Type.Results) {
continue
}

failures = append(failures, lint.Failure{
Confidence: 0.8,
Node: fd,
Category: "logic",
Failure: fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name),
})
}

w := lintReturnRule{onFailure}
ast.Walk(w, file.AST)
return failures
}

Expand All @@ -29,10 +45,6 @@ func (*GetReturnRule) Name() string {
return "get-return"
}

type lintReturnRule struct {
onFailure func(lint.Failure)
}

const getterPrefix = "GET"

var lenGetterPrefix = len(getterPrefix)
Expand All @@ -57,24 +69,3 @@ func isGetter(name string) bool {
func hasResults(rs *ast.FieldList) bool {
return rs != nil && len(rs.List) > 0
}

func (w lintReturnRule) Visit(node ast.Node) ast.Visitor {
fd, ok := node.(*ast.FuncDecl)
if !ok {
return w
}

if !isGetter(fd.Name.Name) {
return w
}
if !hasResults(fd.Type.Results) {
w.onFailure(lint.Failure{
Confidence: 0.8,
Node: fd,
Category: "logic",
Failure: fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name),
})
}

return w
}

0 comments on commit 3e1dbc0

Please sign in to comment.