8000 Check the reachable time.Tick receive branch by dajiaohuang · Pull Request #1809 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions go/ir/irutil/terminates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package irutil

import (
"go/token"
"go/types"

"honnef.co/go/tools/go/ir"
Expand Down Expand Up @@ -29,15 +30,14 @@ func Terminates(fn *ir.Function) bool {
// Check if we got here by receiving from a closed
// time.Tick channel – this cannot happen at
// runtime and thus doesn't constitute termination
iff := ctrl
if !ok {
return true
}
ex, ok := iff.Cond.(*ir.Extract)
if !ok {
return true
cond := ctrl.Cond
negated := false
if unop, ok := cond.(*ir.UnOp); ok && unop.Op == token.NOT {
cond = unop.X
negated = true
}
if ex.Index != 1 {
ex, ok := cond.(*ir.Extract)
if !ok || ex.Index != 1 {
return true
}
recv, ok := ex.Tuple.(*ir.Recv)
Expand All @@ -59,6 +59,22 @@ func Terminates(fn *ir.Function) bool {
if fn2.FullName() != "time.Tick" {
return true
}
// A receive from time.Tick always reports ok == true. The
// return is reachable only on the true edge, accounting for
// an optional logical negation of the condition.
found := false
for i, succ := range pred.Succs {
if succ == block {
found = true
if (i == 0) != negated {
return true
}
break
}
}
if !found {
return true
}
default:
// we've reached the exit block
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ func fn5() {
}
}

func fnTickOKBranchReturn() {
for {
_, ok := <-time.Tick(time.Second) //@ diag(`leaks the underlying ticker`)
if ok {
return
}
}
}

func fnTickClosedBranchReturn() {
for {
_, ok := <-time.Tick(time.Second)
if !ok {
return
}
}
}

type T struct{}

Expand Down
0