From 58f8a7e5ac5d0700dbc3c363cc46233af3094c34 Mon Sep 17 00:00:00 2001 From: chavacava Date: Wed, 4 Dec 2024 13:55:16 +0100 Subject: [PATCH 1/3] refactor (rule/function-result-limit): replace AST walker by iteration over declarations --- rule/function_result_limit.go | 52 ++++++++++++----------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index bd4fe377f..1b102dd3d 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -39,47 +39,31 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure + for _, decl := range file.AST.Decls { + funcDecl, ok := decl.(*ast.FuncDecl) + if !ok { + continue + } - walker := lintFunctionResultsNum{ - max: r.max, - onFailure: func(failure lint.Failure) { - failures = append(failures, failure) - }, - } - - ast.Walk(walker, file.AST) - - return failures -} - -// Name returns the rule name. -func (*FunctionResultsLimitRule) Name() string { - return "function-result-limit" -} - -type lintFunctionResultsNum struct { - max int - onFailure func(lint.Failure) -} - -func (w lintFunctionResultsNum) Visit(n ast.Node) ast.Visitor { - node, ok := n.(*ast.FuncDecl) - if ok { num := 0 - hasResults := node.Type.Results != nil + hasResults := funcDecl.Type.Results != nil if hasResults { - num = node.Type.Results.NumFields() + num = funcDecl.Type.Results.NumFields() } - if num > w.max { - w.onFailure(lint.Failure{ + + if num > r.max { + failures = append(failures, lint.Failure{ Confidence: 1, - Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", w.max, num), - Node: node.Type, + Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", r.max, num), + Node: funcDecl.Type, }) } - - return nil // skip visiting function's body } - return w + return failures +} + +// Name returns the rule name. +func (*FunctionResultsLimitRule) Name() string { + return "function-result-limit" } From 57e4695e4da00ecf5a5553d4d4488f6c8c54f597 Mon Sep 17 00:00:00 2001 From: chavacava Date: Wed, 4 Dec 2024 13:58:05 +0100 Subject: [PATCH 2/3] moves auxiliary func to the end of the source file --- rule/function_result_limit.go | 38 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 1b102dd3d..953f33bab 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -15,25 +15,6 @@ type FunctionResultsLimitRule struct { configureOnce sync.Once } -const defaultResultsLimit = 3 - -func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) { - if len(arguments) < 1 { - r.max = defaultResultsLimit - return - } - - maxResults, ok := arguments[0].(int64) // Alt. non panicking version - if !ok { - panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0])) - } - if maxResults < 0 { - panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) - } - - r.max = int(maxResults) -} - // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { r.configureOnce.Do(func() { r.configure(arguments) }) @@ -67,3 +48,22 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen func (*FunctionResultsLimitRule) Name() string { return "function-result-limit" } + +const defaultResultsLimit = 3 + +func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) { + if len(arguments) < 1 { + r.max = defaultResultsLimit + return + } + + maxResults, ok := arguments[0].(int64) // Alt. non panicking version + if !ok { + panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0])) + } + if maxResults < 0 { + panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) + } + + r.max = int(maxResults) +} From c17efaf36292435392e97a3e489bd6988cd55daa Mon Sep 17 00:00:00 2001 From: chavacava Date: Wed, 4 Dec 2024 21:08:05 +0100 Subject: [PATCH 3/3] nit: failure in the happy path --- rule/function_result_limit.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 953f33bab..06865457c 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -32,13 +32,15 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen num = funcDecl.Type.Results.NumFields() } - if num > r.max { - failures = append(failures, lint.Failure{ - Confidence: 1, - Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", r.max, num), - Node: funcDecl.Type, - }) + if num <= r.max { + continue } + + failures = append(failures, lint.Failure{ + Confidence: 1, + Failure: fmt.Sprintf("maximum number of return results per function exceeded; max %d but got %d", r.max, num), + Node: funcDecl.Type, + }) } return failures