Skip to content

Commit

Permalink
fix: Cycle Detection to Ignore Direct Recursion (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon authored Aug 1, 2024
1 parent 6dbb8d5 commit 3843478
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 1 addition & 2 deletions internal/lints/detect_cycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<address> outer]
// [outer outer]
t.Errorf("unexpected result: %v", result)
}
}
6 changes: 4 additions & 2 deletions internal/lints/detect_cycles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3843478

Please sign in to comment.