Skip to content

Commit

Permalink
Fix gocognit output when a function has generic receiver type
Browse files Browse the repository at this point in the history
recvString func in gocognit returns BADRECV when the function's receiver is a
generic type.
  • Loading branch information
ivanruski committed Jun 23, 2022
1 parent 8514510 commit 6ed2d52
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gocognit.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/ast"
"go/token"
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
Expand Down Expand Up @@ -57,6 +58,15 @@ func recvString(recv ast.Expr) string {
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
case *ast.IndexExpr:
return recvString(t.X) + "[" + recvString(t.Index) + "]"
case *ast.IndexListExpr:
targs := make([]string, 0, len(t.Indices))
for _, exp := range t.Indices {
targs = append(targs, recvString(exp))
}

return recvString(t.X) + "[" + strings.Join(targs, ", ") + "]"
}
return "BADRECV"
}
Expand Down

0 comments on commit 6ed2d52

Please sign in to comment.