Skip to content

Commit 150dee4

Browse files
authored
Merge pull request #10 from ivanruski/master
Fix gocognit output when a function has generic receiver type
2 parents 90b29bc + c660021 commit 150dee4

File tree

5 files changed

+103
-12
lines changed

5 files changed

+103
-12
lines changed

gocognit.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ func funcName(fn *ast.FuncDecl) string {
4949
return fn.Name.Name
5050
}
5151

52-
// recvString returns a string representation of recv of the
53-
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
54-
func recvString(recv ast.Expr) string {
55-
switch t := recv.(type) {
56-
case *ast.Ident:
57-
return t.Name
58-
case *ast.StarExpr:
59-
return "*" + recvString(t.X)
60-
}
61-
return "BADRECV"
62-
}
63-
6452
// Complexity calculates the cognitive complexity of a function.
6553
func Complexity(fn *ast.FuncDecl) int {
6654
v := complexityVisitor{

gocognit118_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package gocognit_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/uudashr/gocognit"
10+
"golang.org/x/tools/go/analysis/analysistest"
11+
)
12+
13+
func TestAnalyzer_Generics(t *testing.T) {
14+
testdata := analysistest.TestData()
15+
gocognit.Analyzer.Flags.Set("over", "0")
16+
analysistest.Run(t, testdata, gocognit.Analyzer, "c")
17+
}

recv.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package gocognit
5+
6+
import (
7+
"go/ast"
8+
"strings"
9+
)
10+
11+
// 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).
13+
func recvString(recv ast.Expr) string {
14+
switch t := recv.(type) {
15+
case *ast.Ident:
16+
return t.Name
17+
case *ast.StarExpr:
18+
return "*" + recvString(t.X)
19+
case *ast.IndexExpr:
20+
return recvString(t.X) + "[" + recvString(t.Index) + "]"
21+
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, ", ") + "]"
28+
}
29+
return "BADRECV"
30+
}

recv_pre118.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !go1.18
2+
// +build !go1.18
3+
4+
package gocognit
5+
6+
import (
7+
"go/ast"
8+
)
9+
10+
// recvString returns a string representation of recv of the
11+
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
12+
func recvString(recv ast.Expr) string {
13+
switch t := recv.(type) {
14+
case *ast.Ident:
15+
return t.Name
16+
case *ast.StarExpr:
17+
return "*" + recvString(t.X)
18+
}
19+
return "BADRECV"
20+
}

testdata/src/c/c.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package testdata
2+
3+
type Node[T any] struct {
4+
}
5+
6+
func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\[T\\]\\)\\.String is high \\(> 0\\)"
7+
if n != nil { // +1
8+
return "Node"
9+
}
10+
11+
return ""
12+
} // total complexity = 1
13+
14+
type Pair[K any, V any] struct {
15+
Key K
16+
Value V
17+
}
18+
19+
func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\[K, V\\]\\)\\.String is high \\(> 0\\)"
20+
if p != nil { // +1
21+
return "Pair"
22+
}
23+
24+
return ""
25+
} // total complexity = 1
26+
27+
type Triple[K any, V any, T any] struct {
28+
}
29+
30+
func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\[K, V, T\\]\\)\\.String is high \\(> 0\\)"
31+
if t != nil { // +1 `
32+
return "Triple"
33+
}
34+
35+
return ""
36+
} // total complexity = 1

0 commit comments

Comments
 (0)