mirror of
https://github.com/mgechev/revive.git
synced 2025-02-15 13:53:15 +02:00
New rule: confusing-results (#27)
* Adds rule superfluous-else (an extension of indent-error-flow) * Fix superfluous-else rule struct namming. * Adds superfuous-else rule to the rules table * Adds confusing-naming rule * adds multifile test * clean-up * fix config.go * confusing-results working version
This commit is contained in:
parent
443bfc9e0b
commit
b8eababb0d
@ -213,6 +213,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
|||||||
| `confusing-naming` | n/a | Warns on methods with names that differ only by capitalization | no | no |
|
| `confusing-naming` | n/a | Warns on methods with names that differ only by capitalization | no | no |
|
||||||
| `get-return ` | n/a | Warns on getters that do not yield any result | no | no |
|
| `get-return ` | n/a | Warns on getters that do not yield any result | no | no |
|
||||||
| `modifies-param` | n/a | Warns on assignments to function parameters | no | no |
|
| `modifies-param` | n/a | Warns on assignments to function parameters | no | no |
|
||||||
|
| `confusing-results` | n/a | Suggests to name potentially confusing function results | no | no |
|
||||||
| `deep-exit` | n/a | Looks for program exits in funcs other than `main()` or `init()` | no | no |
|
| `deep-exit` | n/a | Looks for program exits in funcs other than `main()` or `init()` | no | no |
|
||||||
|
|
||||||
## Available Formatters
|
## Available Formatters
|
||||||
|
@ -52,6 +52,7 @@ var allRules = append([]lint.Rule{
|
|||||||
&rule.ConfusingNamingRule{},
|
&rule.ConfusingNamingRule{},
|
||||||
&rule.GetReturnRule{},
|
&rule.GetReturnRule{},
|
||||||
&rule.ModifiesParamRule{},
|
&rule.ModifiesParamRule{},
|
||||||
|
&rule.ConfusingResultsRule{},
|
||||||
&rule.DeepExitRule{},
|
&rule.DeepExitRule{},
|
||||||
}, defaultRules...)
|
}, defaultRules...)
|
||||||
|
|
||||||
|
20
fixtures/confusing-results.go
Normal file
20
fixtures/confusing-results.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package fixtures
|
||||||
|
|
||||||
|
func getfoo() (int, int, error) { // MATCH /unnamed results of the same type may be confusing, consider using named results/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBar(a, b int) (int, error, int) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func Getbaz(a string, b int) (int, float32, string, string) { // MATCH /unnamed results of the same type may be confusing, consider using named results/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTaz(a string, b int) string {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *t) GetTaz(a int, b int) {
|
||||||
|
|
||||||
|
}
|
68
rule/confusing-results.go
Normal file
68
rule/confusing-results.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/ast"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConfusingResultsRule lints given function declarations
|
||||||
|
type ConfusingResultsRule struct{}
|
||||||
|
|
||||||
|
// Apply applies the rule to given file.
|
||||||
|
func (r *ConfusingResultsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||||
|
var failures []lint.Failure
|
||||||
|
|
||||||
|
fileAst := file.AST
|
||||||
|
walker := lintConfusingResults{
|
||||||
|
onFailure: func(failure lint.Failure) {
|
||||||
|
failures = append(failures, failure)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ast.Walk(walker, fileAst)
|
||||||
|
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the rule name.
|
||||||
|
func (r *ConfusingResultsRule) Name() string {
|
||||||
|
return "confusing-results"
|
||||||
|
}
|
||||||
|
|
||||||
|
type lintConfusingResults struct {
|
||||||
|
onFailure func(lint.Failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w lintConfusingResults) Visit(n ast.Node) ast.Visitor {
|
||||||
|
fn, ok := n.(*ast.FuncDecl)
|
||||||
|
if !ok || fn.Type.Results == nil || len(fn.Type.Results.List) < 2 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
lastType := ""
|
||||||
|
for _, result := range fn.Type.Results.List {
|
||||||
|
if len(result.Names) > 0 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
t, ok := result.Type.(*ast.Ident)
|
||||||
|
if !ok {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.Name == lastType {
|
||||||
|
w.onFailure(lint.Failure{
|
||||||
|
Node: n,
|
||||||
|
Confidence: 1,
|
||||||
|
URL: "#named-result-parameters",
|
||||||
|
Category: "naming",
|
||||||
|
Failure: "unnamed results of the same type may be confusing, consider using named results",
|
||||||
|
})
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
lastType = t.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
|
}
|
11
test/confusing-results_test.go
Normal file
11
test/confusing-results_test.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/rule"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConfusingResults(t *testing.T) {
|
||||||
|
testRule(t, "confusing-results", &rule.ConfusingResultsRule{})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user