2018-09-17 11:06:53 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2022-04-10 09:06:59 +02:00
|
|
|
"sync"
|
2018-09-17 11:06:53 +02:00
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FunctionResultsLimitRule lints given else constructs.
|
2021-10-17 20:34:48 +02:00
|
|
|
type FunctionResultsLimitRule struct {
|
|
|
|
max int
|
2022-04-10 09:06:59 +02:00
|
|
|
sync.Mutex
|
2021-10-17 20:34:48 +02:00
|
|
|
}
|
2018-09-17 11:06:53 +02:00
|
|
|
|
2023-05-20 14:44:34 +02:00
|
|
|
const defaultResultsLimit = 3
|
|
|
|
|
2022-04-10 09:06:59 +02:00
|
|
|
func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) {
|
|
|
|
r.Lock()
|
2023-05-20 14:44:34 +02:00
|
|
|
defer r.Unlock()
|
2024-10-01 12:14:02 +02:00
|
|
|
if r.max != 0 {
|
|
|
|
return // already configured
|
2018-09-17 11:06:53 +02:00
|
|
|
}
|
2024-10-01 12:14:02 +02:00
|
|
|
|
|
|
|
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)
|
2022-04-10 09:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
|
|
|
func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
|
|
|
r.configure(arguments)
|
2018-09-17 11:06:53 +02:00
|
|
|
|
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
walker := lintFunctionResultsNum{
|
2021-10-17 20:34:48 +02:00
|
|
|
max: r.max,
|
2018-09-17 11:06:53 +02:00
|
|
|
onFailure: func(failure lint.Failure) {
|
|
|
|
failures = append(failures, failure)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(walker, file.AST)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*FunctionResultsLimitRule) Name() string {
|
2018-09-17 11:06:53 +02:00
|
|
|
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
|
2024-10-01 12:14:02 +02:00
|
|
|
hasResults := node.Type.Results != nil
|
|
|
|
if hasResults {
|
2018-09-17 11:06:53 +02:00
|
|
|
num = node.Type.Results.NumFields()
|
|
|
|
}
|
|
|
|
if num > w.max {
|
|
|
|
w.onFailure(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,
|
|
|
|
})
|
|
|
|
}
|
2024-10-01 12:14:02 +02:00
|
|
|
|
|
|
|
return nil // skip visiting function's body
|
2018-09-17 11:06:53 +02:00
|
|
|
}
|
2024-10-01 12:14:02 +02:00
|
|
|
|
2018-09-17 11:06:53 +02:00
|
|
|
return w
|
|
|
|
}
|