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. |
4 | |
5 | package a |
6 | |
7 | import ( |
8 | "context" |
9 | "log" |
10 | "os" |
11 | "testing" |
12 | "time" |
13 | ) |
14 | |
15 | var bg = context.Background() |
16 | |
17 | // Check the three functions and assignment forms (var, :=, =) we look for. |
18 | // (Do these early: line numbers are fragile.) |
19 | func _() { |
20 | var _, cancel = context.WithCancel(bg) // want `the cancel function is not used on all paths \(possible context leak\)` |
21 | if false { |
22 | _ = cancel |
23 | } |
24 | } // want "this return statement may be reached without using the cancel var defined on line 20" |
25 | |
26 | func _() { |
27 | _, cancel2 := context.WithDeadline(bg, time.Time{}) // want "the cancel2 function is not used..." |
28 | if false { |
29 | _ = cancel2 |
30 | } |
31 | } // want "may be reached without using the cancel2 var defined on line 27" |
32 | |
33 | func _() { |
34 | var cancel3 func() |
35 | _, cancel3 = context.WithTimeout(bg, 0) // want "function is not used..." |
36 | if false { |
37 | _ = cancel3 |
38 | } |
39 | } // want "this return statement may be reached without using the cancel3 var defined on line 35" |
40 | |
41 | func _() { |
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" |
45 | _ = ctx |
46 | } |
47 | |
48 | func _() { |
49 | _, cancel := context.WithCancel(bg) |
50 | defer cancel() // ok |
51 | } |
52 | |
53 | func _() { |
54 | _, cancel := context.WithCancel(bg) // want "not used on all paths" |
55 | if condition { |
56 | cancel() |
57 | } |
58 | return // want "this return statement may be reached without using the cancel var" |
59 | } |
60 | |
61 | func _() { |
62 | _, cancel := context.WithCancel(bg) |
63 | if condition { |
64 | cancel() |
65 | } else { |
66 | // ok: infinite loop |
67 | for { |
68 | print(0) |
69 | } |
70 | } |
71 | } |
72 | |
73 | func _() { |
74 | _, cancel := context.WithCancel(bg) // want "not used on all paths" |
75 | if condition { |
76 | cancel() |
77 | } else { |
78 | for i := 0; i < 10; i++ { |
79 | print(0) |
80 | } |
81 | } |
82 | } // want "this return statement may be reached without using the cancel var" |
83 | |
84 | func _() { |
85 | _, cancel := context.WithCancel(bg) |
86 | // ok: used on all paths |
87 | switch someInt { |
88 | case 0: |
89 | new(testing.T).FailNow() |
90 | case 1: |
91 | log.Fatal() |
92 | case 2: |
93 | cancel() |
94 | case 3: |
95 | print("hi") |
96 | fallthrough |
97 | default: |
98 | os.Exit(1) |
99 | } |
100 | } |
101 | |
102 | func _() { |
103 | _, cancel := context.WithCancel(bg) // want "not used on all paths" |
104 | switch someInt { |
105 | case 0: |
106 | new(testing.T).FailNow() |
107 | case 1: |
108 | log.Fatal() |
109 | case 2: |
110 | cancel() |
111 | case 3: |
112 | print("hi") // falls through to implicit return |
113 | default: |
114 | os.Exit(1) |
115 | } |
116 | } // want "this return statement may be reached without using the cancel var" |
117 | |
118 | func _(ch chan int) { |
119 | _, cancel := context.WithCancel(bg) // want "not used on all paths" |
120 | select { |
121 | case <-ch: |
122 | new(testing.T).FailNow() |
123 | case ch <- 2: |
124 | print("hi") // falls through to implicit return |
125 | case ch <- 1: |
126 | cancel() |
127 | default: |
128 | os.Exit(1) |
129 | } |
130 | } // want "this return statement may be reached without using the cancel var" |
131 | |
132 | func _(ch chan int) { |
133 | _, cancel := context.WithCancel(bg) |
134 | // A blocking select must execute one of its cases. |
135 | select { |
136 | case <-ch: |
137 | panic(0) |
138 | } |
139 | if false { |
140 | _ = cancel |
141 | } |
142 | } |
143 | |
144 | func _() { |
145 | go func() { |
146 | ctx, cancel := context.WithCancel(bg) // want "not used on all paths" |
147 | if false { |
148 | _ = cancel |
149 | } |
150 | print(ctx) |
151 | }() // want "may be reached without using the cancel var" |
152 | } |
153 | |
154 | var condition bool |
155 | var someInt int |
156 | |
157 | // Regression test for Go issue 16143. |
158 | func _() { |
159 | var x struct{ f func() } |
160 | x.f() |
161 | } |
162 | |
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 |
167 | } |
168 | |
169 | // Same as above, but for literal function. |
170 | var _ = func() (ctx context.Context, cancel func()) { |
171 | ctx, cancel = context.WithCancel(bg) |
172 | return |
173 | } |
174 | |
175 | // Test for Go issue 31856. |
176 | func _() { |
177 | var cancel func() |
178 | |
179 | func() { |
180 | _, cancel = context.WithCancel(bg) |
181 | }() |
182 | |
183 | cancel() |
184 | } |
185 | |
186 | var cancel1 func() |
187 | |
188 | // Same as above, but for package-level cancel variable. |
189 | func _() { |
190 | // We assume that other uses of cancel1 exist. |
191 | _, cancel1 = context.WithCancel(bg) |
192 | } |
193 |
Members