mirror of
https://github.com/mgechev/revive.git
synced 2024-11-28 08:49:11 +02:00
New rule: return-argument-limit
This commit is contained in:
parent
8d6642ccea
commit
f0ec17c8d5
@ -269,6 +269,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
||||
| `constant-logical-expr` | n/a | Warns on constant logical expressions | no | no |
|
||||
| `bool-literal-in-expr`| n/a | Suggests removing Boolean literals from logic expressions | no | no |
|
||||
| `redefines-builtin-id`| n/a | Warns on redefinitions of builtin identifiers | no | no |
|
||||
| `return-argument-limit` | int | Specifies the maximum number of arguments a function can return | no | no |
|
||||
|
||||
## Available Formatters
|
||||
|
||||
|
@ -64,6 +64,7 @@ var allRules = append([]lint.Rule{
|
||||
&rule.ConstantLogicalExprRule{},
|
||||
&rule.BoolLiteralRule{},
|
||||
&rule.RedefinesBuiltinIDRule{},
|
||||
&rule.ReturnArgumentsLimitRule{},
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
|
17
fixtures/return-argument-limit.go
Normal file
17
fixtures/return-argument-limit.go
Normal file
@ -0,0 +1,17 @@
|
||||
package fixtures
|
||||
|
||||
func foo() (a, b, c, d) { // MATCH /maximum number of return argument per function exceeded; max 3 but got 4/
|
||||
var a, b, c, d int
|
||||
}
|
||||
|
||||
func bar(a, b int) {
|
||||
|
||||
}
|
||||
|
||||
func baz(a string, b int) {
|
||||
|
||||
}
|
||||
|
||||
func qux() (string, string, int, string, int) { // MATCH /maximum number of return argument per function exceeded; max 3 but got 5/
|
||||
|
||||
}
|
65
rule/return-argument-limit.go
Normal file
65
rule/return-argument-limit.go
Normal file
@ -0,0 +1,65 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// ReturnArgumentsLimitRule lints given else constructs.
|
||||
type ReturnArgumentsLimitRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *ReturnArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
if len(arguments) != 1 {
|
||||
panic(`invalid configuration for "return-argument-limit"`)
|
||||
}
|
||||
|
||||
total, ok := arguments[0].(int64) // Alt. non panicking version
|
||||
if !ok {
|
||||
panic(`invalid value passed as return argument number to the "return-argument-limit" rule`)
|
||||
}
|
||||
|
||||
var failures []lint.Failure
|
||||
|
||||
walker := lintReturnArgumentsNum{
|
||||
total: int(total),
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
}
|
||||
|
||||
ast.Walk(walker, file.AST)
|
||||
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *ReturnArgumentsLimitRule) Name() string {
|
||||
return "return-argument-limit"
|
||||
}
|
||||
|
||||
type lintReturnArgumentsNum struct {
|
||||
total int
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w lintReturnArgumentsNum) Visit(n ast.Node) ast.Visitor {
|
||||
node, ok := n.(*ast.FuncDecl)
|
||||
if ok {
|
||||
num := 0
|
||||
if node.Type.Results != nil {
|
||||
num = node.Type.Results.NumFields()
|
||||
}
|
||||
if num > w.total {
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
Failure: fmt.Sprintf("maximum number of return argument per function exceeded; max %d but got %d", w.total, num),
|
||||
Node: node.Type,
|
||||
})
|
||||
return w
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
Loading…
Reference in New Issue
Block a user