Skip to content

Commit

Permalink
Fix gocognit output for generic receivers with multiple type params
Browse files Browse the repository at this point in the history
When the receiver type was a generic type with multiple type parameters, the
output of recvString was "BADRECV". This was happening because expression for
generic type of one param has different type than expression for generic type
with multiple params.
  • Loading branch information
ivanruski committed Jun 22, 2022
1 parent 9370605 commit d79a89f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 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 @@ -59,6 +60,13 @@ func recvString(recv ast.Expr) string {
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 d79a89f

Please sign in to comment.