Skip to content

Commit

Permalink
Merge pull request #10 from ivanruski/master
Browse files Browse the repository at this point in the history
Fix gocognit output when a function has generic receiver type
  • Loading branch information
uudashr committed Jul 1, 2022
2 parents 90b29bc + c660021 commit 150dee4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
12 changes: 0 additions & 12 deletions gocognit.go
Expand Up @@ -49,18 +49,6 @@ func funcName(fn *ast.FuncDecl) string {
return fn.Name.Name
}

// recvString returns a string representation of recv of the
// 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)
}
return "BADRECV"
}

// Complexity calculates the cognitive complexity of a function.
func Complexity(fn *ast.FuncDecl) int {
v := complexityVisitor{
Expand Down
17 changes: 17 additions & 0 deletions gocognit118_test.go
@@ -0,0 +1,17 @@
//go:build go1.18
// +build go1.18

package gocognit_test

import (
"testing"

"github.com/uudashr/gocognit"
"golang.org/x/tools/go/analysis/analysistest"
)

func TestAnalyzer_Generics(t *testing.T) {
testdata := analysistest.TestData()
gocognit.Analyzer.Flags.Set("over", "0")
analysistest.Run(t, testdata, gocognit.Analyzer, "c")
}
30 changes: 30 additions & 0 deletions recv.go
@@ -0,0 +1,30 @@
//go:build go1.18
// +build go1.18

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).
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) + "]"
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 "BADRECV"
}
20 changes: 20 additions & 0 deletions recv_pre118.go
@@ -0,0 +1,20 @@
//go:build !go1.18
// +build !go1.18

package gocognit

import (
"go/ast"
)

// recvString returns a string representation of recv of the
// 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)
}
return "BADRECV"
}
36 changes: 36 additions & 0 deletions testdata/src/c/c.go
@@ -0,0 +1,36 @@
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\\)"
if n != nil { // +1
return "Node"
}

return ""
} // total complexity = 1

type Pair[K any, V any] struct {
Key K
Value V
}

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

return ""
} // total complexity = 1

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\\)"
if t != nil { // +1 `
return "Triple"
}

return ""
} // total complexity = 1

0 comments on commit 150dee4

Please sign in to comment.