From d79a89f48efa6fdb8602fb547635d44b0ddff20c Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Wed, 22 Jun 2022 08:21:45 +0300 Subject: [PATCH] Fix gocognit output for generic receivers with multiple type params 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. --- gocognit.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gocognit.go b/gocognit.go index 30c770c..a36a693 100644 --- a/gocognit.go +++ b/gocognit.go @@ -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" @@ -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" }