1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 var bg = context.Background()
17 // Check the three functions and assignment forms (var, :=, =) we look for.
18 // (Do these early: line numbers are fragile.)
20 var _, cancel = context.WithCancel(bg) // want `the cancel function is not used on all paths \(possible context leak\)`
24 } // want "this return statement may be reached without using the cancel var defined on line 20"
27 _, cancel2 := context.WithDeadline(bg, time.Time{}) // want "the cancel2 function is not used..."
31 } // want "may be reached without using the cancel2 var defined on line 27"
35 _, cancel3 = context.WithTimeout(bg, 0) // want "function is not used..."
39 } // want "this return statement may be reached without using the cancel3 var defined on line 35"
42 ctx, _ := context.WithCancel(bg) // want "the cancel function returned by context.WithCancel should be called, not discarded, to avoid a context leak"
43 ctx, _ = context.WithTimeout(bg, 0) // want "the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak"
44 ctx, _ = context.WithDeadline(bg, time.Time{}) // want "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
49 _, cancel := context.WithCancel(bg)
54 _, cancel := context.WithCancel(bg) // want "not used on all paths"
58 return // want "this return statement may be reached without using the cancel var"
62 _, cancel := context.WithCancel(bg)
74 _, cancel := context.WithCancel(bg) // want "not used on all paths"
78 for i := 0; i < 10; i++ {
82 } // want "this return statement may be reached without using the cancel var"
85 _, cancel := context.WithCancel(bg)
86 // ok: used on all paths
89 new(testing.T).FailNow()
103 _, cancel := context.WithCancel(bg) // want "not used on all paths"
106 new(testing.T).FailNow()
112 print("hi") // falls through to implicit return
116 } // want "this return statement may be reached without using the cancel var"
118 func _(ch chan int) {
119 _, cancel := context.WithCancel(bg) // want "not used on all paths"
122 new(testing.T).FailNow()
124 print("hi") // falls through to implicit return
130 } // want "this return statement may be reached without using the cancel var"
132 func _(ch chan int) {
133 _, cancel := context.WithCancel(bg)
134 // A blocking select must execute one of its cases.
146 ctx, cancel := context.WithCancel(bg) // want "not used on all paths"
151 }() // want "may be reached without using the cancel var"
157 // Regression test for Go issue 16143.
159 var x struct{ f func() }
163 // Regression test for Go issue 16230.
164 func _() (ctx context.Context, cancel func()) {
165 ctx, cancel = context.WithCancel(bg)
166 return // a naked return counts as a load of the named result values
169 // Same as above, but for literal function.
170 var _ = func() (ctx context.Context, cancel func()) {
171 ctx, cancel = context.WithCancel(bg)
175 // Test for Go issue 31856.
180 _, cancel = context.WithCancel(bg)
188 // Same as above, but for package-level cancel variable.
190 // We assume that other uses of cancel1 exist.
191 _, cancel1 = context.WithCancel(bg)