diff --git a/go/ir/irutil/terminates.go b/go/ir/irutil/terminates.go index 84e7503bb..80e6c0b1f 100644 --- a/go/ir/irutil/terminates.go +++ b/go/ir/irutil/terminates.go @@ -1,6 +1,7 @@ package irutil import ( + "go/token" "go/types" "honnef.co/go/tools/go/ir" @@ -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) @@ -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 diff --git a/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go b/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go index 0f0623a1a..010a66e53 100644 --- a/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go +++ b/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go @@ -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{}