Skip to content

Commit 87c0fdc

Browse files
committed
Simplify recvString output
Refactor recvString to return only type's name without it's type parameters when the type is generic. In this way type's string representation will be consistent with function's string representation(gocognit print only function's name even if it's generic one).
1 parent 150dee4 commit 87c0fdc

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

recv.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ package gocognit
55

66
import (
77
"go/ast"
8-
"strings"
98
)
109

1110
// recvString returns a string representation of recv of the
12-
// form "T", "*T", Type[T], Type[T, V], or "BADRECV" (if not a proper receiver type).
11+
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
1312
func recvString(recv ast.Expr) string {
1413
switch t := recv.(type) {
1514
case *ast.Ident:
1615
return t.Name
1716
case *ast.StarExpr:
1817
return "*" + recvString(t.X)
1918
case *ast.IndexExpr:
20-
return recvString(t.X) + "[" + recvString(t.Index) + "]"
19+
return recvString(t.X)
2120
case *ast.IndexListExpr:
22-
targs := make([]string, len(t.Indices))
23-
for i, exp := range t.Indices {
24-
targs[i] = recvString(exp)
25-
}
26-
27-
return recvString(t.X) + "[" + strings.Join(targs, ", ") + "]"
21+
return recvString(t.X)
2822
}
2923
return "BADRECV"
3024
}

testdata/src/c/c.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package testdata
33
type Node[T any] struct {
44
}
55

6-
func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\[T\\]\\)\\.String is high \\(> 0\\)"
6+
func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\)\\.String is high \\(> 0\\)"
77
if n != nil { // +1
88
return "Node"
99
}
@@ -16,7 +16,7 @@ type Pair[K any, V any] struct {
1616
Value V
1717
}
1818

19-
func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\[K, V\\]\\)\\.String is high \\(> 0\\)"
19+
func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\)\\.String is high \\(> 0\\)"
2020
if p != nil { // +1
2121
return "Pair"
2222
}
@@ -27,7 +27,7 @@ func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \
2727
type Triple[K any, V any, T any] struct {
2828
}
2929

30-
func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\[K, V, T\\]\\)\\.String is high \\(> 0\\)"
30+
func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\)\\.String is high \\(> 0\\)"
3131
if t != nil { // +1 `
3232
return "Triple"
3333
}

0 commit comments

Comments
 (0)