diff --git a/rule/get_return.go b/rule/get_return.go index 638c20286..b16e2712a 100644 --- a/rule/get_return.go +++ b/rule/get_return.go @@ -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 } @@ -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) @@ -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 -}