Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify recvString output #13

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions recv.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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