From 90312858be2f5ad3fc59decd367e0d84dc05172b Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 2 Oct 2026 02:49:22 +0800 Subject: [PATCH 1/2] Check the reachable time.Tick receive branch --- go/ir/irutil/terminates.go | 32 ++++++++++++++----- .../CheckLeakyTimeTick/CheckLeakyTimeTick.go | 18 +++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) 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..68c52a710 100644 --- a/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go +++ b/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go @@ -44,6 +44,24 @@ func fn5() { } +func fnTickOKBranchReturn() { + for { + _, ok := <-time.Tick(0) //@ diag(`leaks the underlying ticker`) + if ok { + return + } + } +} + +func fnTickClosedBranchReturn() { + for { + _, ok := <-time.Tick(0) + if !ok { + return + } + } +} + type T struct{} func (t *T) foo() { From 1989af184e5ea208022ed282d227049ebc24123f Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 2 Oct 2026 14:31:54 +0800 Subject: [PATCH 2/2] Use a live tick channel in termination regressions --- .../testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go b/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go index 68c52a710..010a66e53 100644 --- a/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go +++ b/staticcheck/sa1015/testdata/go1.0/CheckLeakyTimeTick/CheckLeakyTimeTick.go @@ -43,10 +43,9 @@ func fn5() { } } - func fnTickOKBranchReturn() { for { - _, ok := <-time.Tick(0) //@ diag(`leaks the underlying ticker`) + _, ok := <-time.Tick(time.Second) //@ diag(`leaks the underlying ticker`) if ok { return } @@ -55,7 +54,7 @@ func fnTickOKBranchReturn() { func fnTickClosedBranchReturn() { for { - _, ok := <-time.Tick(0) + _, ok := <-time.Tick(time.Second) if !ok { return }