-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patha.go
153 lines (111 loc) · 2.93 KB
/
a.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package a // want package:"ctxCheck"
import (
"context"
"net/http"
)
type MyString string
type TestInterface interface {
Test() string
}
type xx struct{}
func newXX() TestInterface {
return &xx{}
}
func (x xx) Test() string {
return ""
}
type MyInt int
func (x MyInt) F() int {
return int(x)
}
func f1(ctx context.Context) {
defer f2(ctx)
go f2(ctx)
f2(ctx)
ctx = context.WithValue(ctx, MyString("aaa"), "aaaaaa")
f2(ctx)
newXX().Test()
f3() // want "Function `f3` should pass the context parameter"
f6() // want "Function `f6->f3` should pass the context parameter"
defer func() {
f2(ctx)
}()
func(ctx context.Context) {
f2(ctx)
}(ctx)
f2(context.Background()) // want "Non-inherited new context, use function like `context.WithXXX` instead"
thunk := MyInt.F
thunk(0)
bound := MyInt(0).F
bound()
}
func f2(ctx context.Context) {}
func f3() {
f2(context.TODO())
}
func f4(ctx context.Context) {
f2(ctx)
ctx = context.Background()
f2(ctx) // want "Non-inherited new context, use function like `context.WithXXX` instead"
}
func f5(ctx context.Context) {
func() {
f2(ctx)
}()
ctx = context.Background() // want "Non-inherited new context, use function like `context.WithXXX` instead"
f2(ctx)
}
func f6() {
f3()
}
func f7(ctx context.Context) {
ctx, cancel := getNewCtx(ctx)
defer cancel()
f2(ctx) // OK
}
func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
return context.WithCancel(ctx)
}
/* ----------------- http ----------------- */
func f8(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}
func f9(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
f8(ctx, w, r)
f8(context.Background(), w, r) // want "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead"
}
func f10(in bool, w http.ResponseWriter, r *http.Request) {
f8(r.Context(), w, r)
f8(context.Background(), w, r)
}
// nolint: contextcheck
func f14(w http.ResponseWriter, r *http.Request, err error) {
f8(r.Context(), w, r)
}
func f11() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
f8(r.Context(), w, r)
f8(context.Background(), w, r) // want "Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead"
f9(w, r)
// f10 should be like `func f10(ctx context.Context, in bool, w http.ResponseWriter, r *http.Request)`
f10(true, w, r) // want "Function `f10` should pass the context parameter"
f14(w, r, nil)
})
}
/* ----------------- generics ----------------- */
type MySlice[T int | float32] []T
func (s MySlice[T]) f12(ctx context.Context) T {
f3() // generics, Block is nil, wont report
var sum T
for _, value := range s {
sum += value
}
return sum
}
func f13[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](ctx context.Context, a, b T) T {
f3() // generics, Block is nil, wont report
if a > b {
return a
}
return b
}