mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
cognitive-complexity: handle direct recursion (#1149)
This commit is contained in:
@@ -67,7 +67,9 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
|
||||
f := w.file
|
||||
for _, decl := range f.AST.Decls {
|
||||
if fn, ok := decl.(*ast.FuncDecl); ok && fn.Body != nil {
|
||||
v := cognitiveComplexityVisitor{}
|
||||
v := cognitiveComplexityVisitor{
|
||||
name: fn.Name,
|
||||
}
|
||||
c := v.subTreeComplexity(fn.Body)
|
||||
if c > w.maxComplexity {
|
||||
w.onFailure(lint.Failure{
|
||||
@@ -82,6 +84,7 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
|
||||
}
|
||||
|
||||
type cognitiveComplexityVisitor struct {
|
||||
name *ast.Ident
|
||||
complexity int
|
||||
nestingLevel int
|
||||
}
|
||||
@@ -125,8 +128,15 @@ func (v *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
|
||||
if n.Label != nil {
|
||||
v.complexity++
|
||||
}
|
||||
case *ast.CallExpr:
|
||||
if ident, ok := n.Fun.(*ast.Ident); ok {
|
||||
if ident.Obj == v.name.Obj && ident.Name == v.name.Name {
|
||||
// called by same function directly (direct recursion)
|
||||
v.complexity++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO handle (at least) direct recursion
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user