mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
fix: confusing-results doesn't work with pointer types (#1158)
Co-authored-by: chavacava <salvador.cavadini@gmail.com>
This commit is contained in:
@@ -13,14 +13,38 @@ type ConfusingResultsRule struct{}
|
||||
func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
fileAst := file.AST
|
||||
walker := lintConfusingResults{
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
for _, decl := range file.AST.Decls {
|
||||
funcDecl, ok := decl.(*ast.FuncDecl)
|
||||
|
||||
isFunctionWithMoreThanOneResult := ok && funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) > 1
|
||||
if !isFunctionWithMoreThanOneResult {
|
||||
continue
|
||||
}
|
||||
|
||||
ast.Walk(walker, fileAst)
|
||||
resultsAreNamed := len(funcDecl.Type.Results.List[0].Names) > 0
|
||||
if resultsAreNamed {
|
||||
continue
|
||||
}
|
||||
|
||||
lastType := ""
|
||||
for _, result := range funcDecl.Type.Results.List {
|
||||
|
||||
resultTypeName := gofmt(result.Type)
|
||||
|
||||
if resultTypeName == lastType {
|
||||
failures = append(failures, lint.Failure{
|
||||
Node: result,
|
||||
Confidence: 1,
|
||||
Category: "naming",
|
||||
Failure: "unnamed results of the same type may be confusing, consider using named results",
|
||||
})
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
lastType = resultTypeName
|
||||
}
|
||||
}
|
||||
|
||||
return failures
|
||||
}
|
||||
@@ -29,38 +53,3 @@ func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai
|
||||
func (*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,
|
||||
Category: "naming",
|
||||
Failure: "unnamed results of the same type may be confusing, consider using named results",
|
||||
})
|
||||
break
|
||||
}
|
||||
lastType = t.Name
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
8
testdata/confusing_results.go
vendored
8
testdata/confusing_results.go
vendored
@@ -18,3 +18,11 @@ func GetTaz(a string, b int) string {
|
||||
func (t *t) GetTaz(a int, b int) {
|
||||
|
||||
}
|
||||
|
||||
func namedResults() (a string, b string) {
|
||||
return "nil", "nil"
|
||||
}
|
||||
|
||||
func pointerResults() (*string, *string) { // MATCH /unnamed results of the same type may be confusing, consider using named results/
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user