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
  • Loading branch information
chavacava committed Dec 4, 2024
1 parent 09fb350 commit d756bc1
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions rule/get_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ 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) {
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 +43,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 +67,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 d756bc1

Please sign in to comment.