Skip to content

Commit db50331

Browse files
committed
Fixed panic due to predefiend func
1 parent f4ff102 commit db50331

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

gocognit.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,16 @@ func (v *complexityVisitor) visitBinaryExpr(n *ast.BinaryExpr) ast.Visitor {
299299
}
300300

301301
func (v *complexityVisitor) visitCallExpr(n *ast.CallExpr) ast.Visitor {
302-
if name, ok := n.Fun.(*ast.Ident); ok {
303-
if name.Obj == v.name.Obj && name.Name == v.name.Name {
302+
if callIdent, ok := n.Fun.(*ast.Ident); ok {
303+
obj, name := callIdent.Obj, callIdent.Name
304+
if obj == v.name.Obj && name == v.name.Name {
304305
// called by same function directly (direct recursion)
305306
v.incComplexity()
306-
} else if fnDecl, ok := name.Obj.Decl.(*ast.FuncDecl); ok {
307-
// called by same function indirectly (indirect recursion)
308-
ast.Walk(v, fnDecl)
307+
} else if obj != nil {
308+
if fnDecl, ok := obj.Decl.(*ast.FuncDecl); ok {
309+
// called by same function indirectly (indirect recursion)
310+
ast.Walk(v, fnDecl)
311+
}
309312
}
310313
}
311314
return v

testdata/src/a/a.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func HelloWorld() string {
10+
_ = len("hello")
1011
return "Hello, World!"
1112
} // total complexity = 0
1213

testdata/src/b/b.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func HelloWorld() string {
10+
_ = len("hello")
1011
return "Hello, World!"
1112
} // total complexity = 0
1213

0 commit comments

Comments
 (0)