GoPLS Viewer

Home|gopls/go/analysis/passes/lostcancel/testdata/src/a/a.go
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
5package a
6
7import (
8    "context"
9    "log"
10    "os"
11    "testing"
12    "time"
13)
14
15var bg = context.Background()
16
17// Check the three functions and assignment forms (var, :=, =) we look for.
18// (Do these early: line numbers are fragile.)
19func _() {
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
26func _() {
27    _cancel2 := context.WithDeadline(bgtime.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
33func _() {
34    var cancel3 func()
35    _cancel3 = context.WithTimeout(bg0// 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
41func _() {
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(bg0)            // want "the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak"
44    ctx_ = context.WithDeadline(bgtime.Time{}) // want "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
45    _ = ctx
46}
47
48func _() {
49    _cancel := context.WithCancel(bg)
50    defer cancel() // ok
51}
52
53func _() {
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
61func _() {
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
73func _() {
74    _cancel := context.WithCancel(bg// want "not used on all paths"
75    if condition {
76        cancel()
77    } else {
78        for i := 0i < 10i++ {
79            print(0)
80        }
81    }
82// want "this return statement may be reached without using the cancel var"
83
84func _() {
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
102func _() {
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
118func _(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
132func _(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
144func _() {
145    go func() {
146        ctxcancel := 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
154var condition bool
155var someInt int
156
157// Regression test for Go issue 16143.
158func _() {
159    var x struct{ f func() }
160    x.f()
161}
162
163// Regression test for Go issue 16230.
164func _() (ctx context.Contextcancel func()) {
165    ctxcancel = 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.
170var _ = func() (ctx context.Contextcancel func()) {
171    ctxcancel = context.WithCancel(bg)
172    return
173}
174
175// Test for Go issue 31856.
176func _() {
177    var cancel func()
178
179    func() {
180        _cancel = context.WithCancel(bg)
181    }()
182
183    cancel()
184}
185
186var cancel1 func()
187
188// Same as above, but for package-level cancel variable.
189func _() {
190    // We assume that other uses of cancel1 exist.
191    _cancel1 = context.WithCancel(bg)
192}
193
MembersX
someInt
context
log
time
_.BlockStmt.BlockStmt.i
condition
_
_.BlockStmt._
_.BlockStmt.BlockStmt.ctx
_.ctx
_.cancel
testing
_.BlockStmt.cancel2
_.BlockStmt.cancel
_.ch
_.BlockStmt.BlockStmt.cancel
os
_.BlockStmt.ctx
Members
X