File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -67,7 +67,9 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
67
67
f := w .file
68
68
for _ , decl := range f .AST .Decls {
69
69
if fn , ok := decl .(* ast.FuncDecl ); ok && fn .Body != nil {
70
- v := cognitiveComplexityVisitor {}
70
+ v := cognitiveComplexityVisitor {
71
+ name : fn .Name ,
72
+ }
71
73
c := v .subTreeComplexity (fn .Body )
72
74
if c > w .maxComplexity {
73
75
w .onFailure (lint.Failure {
@@ -82,6 +84,7 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
82
84
}
83
85
84
86
type cognitiveComplexityVisitor struct {
87
+ name * ast.Ident
85
88
complexity int
86
89
nestingLevel int
87
90
}
@@ -125,8 +128,15 @@ func (v *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
125
128
if n .Label != nil {
126
129
v .complexity ++
127
130
}
131
+ case * ast.CallExpr :
132
+ if ident , ok := n .Fun .(* ast.Ident ); ok {
133
+ if ident .Obj == v .name .Obj && ident .Name == v .name .Name {
134
+ // called by same function directly (direct recursion)
135
+ v .complexity ++
136
+ return nil
137
+ }
138
+ }
128
139
}
129
- // TODO handle (at least) direct recursion
130
140
131
141
return v
132
142
}
Original file line number Diff line number Diff line change @@ -281,3 +281,13 @@ func (m *Migrator) MigrateIfNeeded(target *EtcdVersionPair) error { // MATCH /fu
281
281
282
282
// no regression test for issue #451
283
283
func myFunc ()
284
+
285
+ // Recursive functions
286
+ func Walk (t * Tree , ch chan int ) { // MATCH /function Walk has cognitive complexity 3 (> max enabled 0)/
287
+ if t == nil { // +1
288
+ return
289
+ }
290
+ Walk (t .Left , ch ) // +1
291
+ ch <- t .Value
292
+ Walk (t .Right , ch ) // +1
293
+ }
You can’t perform that action at this time.
0 commit comments