mirror of
https://github.com/mgechev/revive.git
synced 2024-11-24 08:32:22 +02:00
Merge pull request #62 from xuri/return-value-limit
New rule: return-result-limit
This commit is contained in:
commit
7240bd56d0
@ -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 |
|
||||
| `function-result-limit` | int | Specifies the maximum number of results a function can return | no | no |
|
||||
|
||||
## Configurable rules
|
||||
|
||||
|
@ -64,6 +64,7 @@ var allRules = append([]lint.Rule{
|
||||
&rule.ConstantLogicalExprRule{},
|
||||
&rule.BoolLiteralRule{},
|
||||
&rule.RedefinesBuiltinIDRule{},
|
||||
&rule.FunctionResultsLimitRule{},
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
|
17
fixtures/function-result-limit.go
Normal file
17
fixtures/function-result-limit.go
Normal file
@ -0,0 +1,17 @@
|
||||
package fixtures
|
||||
|
||||
func foo() (a, b, c, d) { // MATCH /maximum number of return results 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 results per function exceeded; max 3 but got 5/
|
||||
|
||||
}
|
68
rule/function-result-limit.go
Normal file
68
rule/function-result-limit.go
Normal file
@ -0,0 +1,68 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// FunctionResultsLimitRule lints given else constructs.
|
||||
type FunctionResultsLimitRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
if len(arguments) != 1 {
|
||||
panic(`invalid configuration for "function-result-limit"`)
|
||||
}
|
||||
|
||||
max, 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 max < 0 {
|
||||
panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`)
|
||||
}
|
||||
|
||||
var failures []lint.Failure
|
||||
|
||||
walker := lintFunctionResultsNum{
|
||||
max: int(max),
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
}
|
||||
|
||||
ast.Walk(walker, file.AST)
|
||||
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *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
|
||||
if node.Type.Results != nil {
|
||||
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,
|
||||
})
|
||||
return w
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
14
test/function-result-limit_test.go
Normal file
14
test/function-result-limit_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestFunctionResultsLimit(t *testing.T) {
|
||||
testRule(t, "function-result-limit", &rule.FunctionResultsLimitRule{}, &lint.RuleConfig{
|
||||
Arguments: []interface{}{int64(3)},
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user