Skip to content

Commit

Permalink
Merge pull request #13 from ivanruski/master
Browse files Browse the repository at this point in the history
Simplify recvString output
  • Loading branch information
uudashr committed Jul 4, 2022
2 parents 150dee4 + 87c0fdc commit a225a32
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
12 changes: 3 additions & 9 deletions recv.go
Expand Up @@ -5,26 +5,20 @@ package gocognit

import (
"go/ast"
"strings"
)

// recvString returns a string representation of recv of the
// form "T", "*T", Type[T], Type[T, V], or "BADRECV" (if not a proper receiver type).
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
case *ast.IndexExpr:
return recvString(t.X) + "[" + recvString(t.Index) + "]"
return recvString(t.X)
case *ast.IndexListExpr:
targs := make([]string, len(t.Indices))
for i, exp := range t.Indices {
targs[i] = recvString(exp)
}

return recvString(t.X) + "[" + strings.Join(targs, ", ") + "]"
return recvString(t.X)
}
return "BADRECV"
}
6 changes: 3 additions & 3 deletions testdata/src/c/c.go
Expand Up @@ -3,7 +3,7 @@ package testdata
type Node[T any] struct {
}

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

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

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

0 comments on commit a225a32

Please sign in to comment.