Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: confusing-results doesn't work with pointer types #1158

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 29 additions & 40 deletions rule/confusing_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,43 @@ 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)

ast.Walk(walker, fileAst)
isFunctionWithMoreThanOneResult := ok && funcDecl.Type.Results != nil && len(funcDecl.Type.Results.List) > 1
if !isFunctionWithMoreThanOneResult {
continue
}

return failures
}
resultsAreNamed := len(funcDecl.Type.Results.List[0].Names) > 0
if resultsAreNamed {
continue
}

// Name returns the rule name.
func (*ConfusingResultsRule) Name() string {
return "confusing-results"
}
lastType := ""
for _, result := range funcDecl.Type.Results.List {

type lintConfusingResults struct {
onFailure func(lint.Failure)
}
resultTypeName := gofmt(result.Type)

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
}
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",
})

t, ok := result.Type.(*ast.Ident)
if !ok {
return w
}
break
}

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 = resultTypeName
}
lastType = t.Name
}

return w
return failures
}

// Name returns the rule name.
func (*ConfusingResultsRule) Name() string {
return "confusing-results"
}
10 changes: 9 additions & 1 deletion testdata/confusing_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func GetTaz(a string, b int) string {

}

func (t *t) GetTaz(a int, b int) {
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
}
Loading