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.
14 // Check the three functions and assignment forms (var, :=, =) we look for.
15 // (Do these early: line numbers are fragile.)
17 var ctx
, cancel
= context
.WithCancel() // ERROR "the cancel function is not used on all paths \(possible context leak\)"
18 } // ERROR "this return statement may be reached without using the cancel var defined on line 17"
21 ctx
, cancel2
:= context
.WithDeadline() // ERROR "the cancel2 function is not used..."
22 } // ERROR "may be reached without using the cancel2 var defined on line 21"
25 var ctx context
.Context
27 ctx
, cancel3
= context
.WithTimeout() // ERROR "function is not used..."
28 } // ERROR "this return statement may be reached without using the cancel3 var defined on line 27"
31 ctx
, _
:= context
.WithCancel() // ERROR "the cancel function returned by context.WithCancel should be called, not discarded, to avoid a context leak"
32 ctx
, _
= context
.WithTimeout() // ERROR "the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak"
33 ctx
, _
= context
.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
37 ctx
, cancel
:= context
.WithCancel()
42 ctx
, cancel
:= context
.WithCancel() // ERROR "not used on all paths"
46 return // ERROR "this return statement may be reached without using the cancel var"
50 ctx
, cancel
:= context
.WithCancel()
62 ctx
, cancel
:= context
.WithCancel() // ERROR "not used on all paths"
66 for i
:= 0; i
< 10; i
++ {
70 } // ERROR "this return statement may be reached without using the cancel var"
73 ctx
, cancel
:= context
.WithCancel()
74 // ok: used on all paths
77 new(testing
.T
).FailNow()
91 ctx
, cancel
:= context
.WithCancel() // ERROR "not used on all paths"
94 new(testing
.T
).FailNow()
100 print("hi") // falls through to implicit return
104 } // ERROR "this return statement may be reached without using the cancel var"
106 func _(ch
chan int) int {
107 ctx
, cancel
:= context
.WithCancel() // ERROR "not used on all paths"
110 new(testing
.T
).FailNow()
112 print("hi") // falls through to implicit return
118 } // ERROR "this return statement may be reached without using the cancel var"
120 func _(ch
chan int) int {
121 ctx
, cancel
:= context
.WithCancel()
122 // A blocking select must execute one of its cases.
131 ctx
, cancel
:= context
.WithCancel() // ERROR "not used on all paths"
133 }() // ERROR "may be reached without using the cancel var"
139 // Regression test for Go issue 16143.
141 var x
struct{ f
func() }
145 // Regression test for Go issue 16230.
146 func _() (ctx context
.Context
, cancel
func()) {
147 ctx
, cancel
= context
.WithCancel()
148 return // a naked return counts as a load of the named result values
151 // Same as above, but for literal function.
152 var _
= func() (ctx context
.Context
, cancel
func()) {
153 ctx
, cancel
= context
.WithCancel()