From 3843478542a522fa7be2ae099b7681f99c1415a1 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 1 Aug 2024 12:12:17 +0900 Subject: [PATCH] fix: Cycle Detection to Ignore Direct Recursion (#44) --- internal/lints/detect_cycle_test.go | 3 +-- internal/lints/detect_cycles.go | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/lints/detect_cycle_test.go b/internal/lints/detect_cycle_test.go index fe41729..761082f 100644 --- a/internal/lints/detect_cycle_test.go +++ b/internal/lints/detect_cycle_test.go @@ -48,13 +48,12 @@ func outer() { cycle := newCycle() result := cycle.detectCycles(f) - if len(result) != 6 { + if len(result) != 4 { // [B A B] // [B A B] // [x y x] // [b a b] // [outer outer$anon
outer] - // [outer outer] t.Errorf("unexpected result: %v", result) } } diff --git a/internal/lints/detect_cycles.go b/internal/lints/detect_cycles.go index 71615b8..698b957 100644 --- a/internal/lints/detect_cycles.go +++ b/internal/lints/detect_cycles.go @@ -48,7 +48,9 @@ func (c *cycle) analyzeFuncDecl(fn *ast.FuncDecl) { switch x := n.(type) { case *ast.CallExpr: if ident, ok := x.Fun.(*ast.Ident); ok { - c.dependencies[name] = append(c.dependencies[name], ident.Name) + if ident.Name == name { + c.dependencies[name] = append(c.dependencies[name], ident.Name) + } } case *ast.FuncLit: c.analyzeFuncLit(x, name) @@ -135,7 +137,7 @@ func (c *cycle) dfs(name string) { for _, dep := range c.dependencies[name] { if !c.visited[dep] { c.dfs(dep) - } else if contains(c.stack, dep) { + } else if contains(c.stack, dep) && dep != name { cycle := append(c.stack[indexOf(c.stack, dep):], dep) res := fmt.Sprintf("%v", cycle) c.cycles = append(c.cycles, res)