From d756bc14da78bf9d42e6e13375cdcc581c0ddcba Mon Sep 17 00:00:00 2001 From: chavacava Date: Wed, 4 Dec 2024 16:25:13 +0100 Subject: [PATCH] refactor (rule/get-return): replace AST walker by iteration over declarations --- rule/get_return.go | 47 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/rule/get_return.go b/rule/get_return.go index 638c20286..e3779bebd 100644 --- a/rule/get_return.go +++ b/rule/get_return.go @@ -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 } @@ -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) @@ -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 -}