1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +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:
chavacava 2018-07-02 04:09:58 +02:00 committed by Minko Gechev
parent 443bfc9e0b
commit b8eababb0d
5 changed files with 101 additions and 0 deletions

View File

@ -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 |
| `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 |
| `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 |
## Available Formatters

View File

@ -52,6 +52,7 @@ var allRules = append([]lint.Rule{
&rule.ConfusingNamingRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.ConfusingResultsRule{},
&rule.DeepExitRule{},
}, defaultRules...)

View 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
View 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
}

View 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{})
}