Debian Codegraph @

go/analysis/passes/nilness/testdata/src/a/a.go

raw symbols SCIP
1 package a
2
3 import (
4 "log"
5 "os"
6 )
7
8 type X struct{ f, g int }
9
10 func f(x, y *X) {
11 if x == nil {
12 print(x.f) // want "nil dereference in field selection"
13 } else {
14 print(x.f)
15 }
16
17 if x == nil {
18 if nil != y {
19 print(1)
20 panic(0)
21 }
22 x.f = 1 // want "nil dereference in field selection"
23 y.f = 1 // want "nil dereference in field selection"
24 }
25
26 var f func()
27 if f == nil { // want "tautological condition: nil == nil"
28 go f() // want "nil dereference in dynamic function call"
29 } else {
30 // This block is unreachable,
31 // so we don't report an error for the
32 // nil dereference in the call.
33 defer f()
34 }
35 }
36
37 func f2(ptr *[3]int, i interface{}) {
38 if ptr != nil {
39 print(ptr[:])
40 *ptr = [3]int{}
41 print(*ptr)
42 } else {
43 print(ptr[:]) // want "nil dereference in slice operation"
44 *ptr = [3]int{} // want "nil dereference in store"
45 print(*ptr) // want "nil dereference in load"
46
47 if ptr != nil { // want "impossible condition: nil != nil"
48 // Dominated by ptr==nil and ptr!=nil,
49 // this block is unreachable.
50 // We do not report errors within it.
51 print(*ptr)
52 }
53 }
54
55 if i != nil {
56 print(i.(interface{ f() }))
57 } else {
58 print(i.(interface{ f() })) // want "nil dereference in type assertion"
59 }
60 }
61
62 func g() error { return nil }
63
64 func f3() error {
65 err := g()
66 if err != nil {
67 return err
68 }
69 if err != nil && err.Error() == "foo" { // want "impossible condition: nil != nil"
70 print(0)
71 }
72 ch := make(chan int)
73 if ch == nil { // want "impossible condition: non-nil == nil"
74 print(0)
75 }
76 if ch != nil { // want "tautological condition: non-nil != nil"
77 print(0)
78 }
79 return nil
80 }
81
82 func h(err error, b bool) {
83 if err != nil && b {
84 return
85 } else if err != nil {
86 panic(err)
87 }
88 }
89
90 func i(*int) error {
91 for {
92 if err := g(); err != nil {
93 return err
94 }
95 }
96 }
97
98 func f4(x *X) {
99 if x == nil {
100 panic(x)
101 }
102 }
103
104 func f5(x *X) {
105 panic(nil) // want "panic with nil value"
106 }
107
108 func f6(x *X) {
109 var err error
110 panic(err) // want "panic with nil value"
111 }
112
113 func f7() {
114 x, err := bad()
115 if err != nil {
116 panic(0)
117 }
118 if x == nil {
119 panic(err) // want "panic with nil value"
120 }
121 }
122
123 func bad() (*X, error) {
124 return nil, nil
125 }
126
127 func f8() {
128 var e error
129 v, _ := e.(interface{})
130 print(v)
131 }
132
133 func f9(x interface {
134 a()
135 b()
136 c()
137 }) {
138 x.b() // we don't catch this panic because we don't have any facts yet
139 xx := interface {
140 a()
141 b()
142 }(x)
143 if xx != nil {
144 return
145 }
146 x.c() // want "nil dereference in dynamic method call"
147 xx.b() // want "nil dereference in dynamic method call"
148 xxx := interface{ a() }(xx)
149 xxx.a() // want "nil dereference in dynamic method call"
150
151 if unknown() {
152 panic(x) // want "panic with nil value"
153 }
154 if unknown() {
155 panic(xx) // want "panic with nil value"
156 }
157 if unknown() {
158 panic(xxx) // want "panic with nil value"
159 }
160 }
161
162 func f10() {
163 s0 := make([]string, 0)
164 if s0 == nil { // want "impossible condition: non-nil == nil"
165 print(0)
166 }
167
168 var s1 []string
169 if s1 == nil { // want "tautological condition: nil == nil"
170 print(0)
171 }
172 s2 := s1[:][:]
173 if s2 == nil { // want "tautological condition: nil == nil"
174 print(0)
175 }
176 }
177
178 func unknown() bool {
179 return false
180 }
181
182 func f11(a interface{}) {
183 switch a.(type) {
184 case nil:
185 return
186 }
187 switch a.(type) {
188 case nil: // want "impossible condition: non-nil == nil"
189 return
190 }
191 }
192
193 func f12(a interface{}) {
194 switch a {
195 case nil:
196 return
197 }
198 switch a {
199 case 5,
200 nil: // want "impossible condition: non-nil == nil"
201 return
202 }
203 }
204
205 type Y struct {
206 innerY
207 }
208
209 type innerY struct {
210 value int
211 }
212
213 func f13() {
214 var d *Y
215 print(d.value) // want "nil dereference in field selection"
216 }
217
218 func f14() {
219 var x struct{ f string }
220 if x == struct{ f string }{} { // we don't catch this tautology as we restrict to reference types
221 print(x)
222 }
223 }
224
225 func f15(x any) {
226 ptr, ok := x.(*int)
227 if ok {
228 return
229 }
230 println(*ptr) // want "nil dereference in load"
231 }
232
233 func f16(x any) {
234 ptr, ok := x.(*int)
235 if !ok {
236 println(*ptr) // want "nil dereference in load"
237 return
238 }
239 println(*ptr)
240 }
241
242 func f18(x any) {
243 ptr, ok := x.(*int)
244 if ok {
245 println(ptr)
246 // falls through
247 }
248 println(*ptr)
249 }
250
251 // Regression test for https://github.com/golang/go/issues/65674:
252 // spurious "nil deference in slice index operation" when the
253 // index was subject to a range loop.
254 func f19(slice []int, array *[2]int, m map[string]int, ch chan int) {
255 if slice == nil {
256 // A range over a nil slice is dynamically benign,
257 // but still signifies a programmer mistake.
258 //
259 // Since SSA has melted down the control structure,
260 // so we can only report a diagnostic about the
261 // index operation, with heuristics for "range".
262
263 for range slice { // nothing to report here
264 }
265 for _, v := range slice { // want "range of nil slice"
266 _ = v
267 }
268 for i := range slice {
269 _ = slice[i] // want "range of nil slice"
270 }
271 {
272 var i int
273 for i = range slice {
274 }
275 _ = slice[i] // want "index of nil slice"
276 }
277 for i := range slice {
278 if i < len(slice) {
279 _ = slice[i] // want "range of nil slice"
280 }
281 }
282 if len(slice) > 3 {
283 _ = slice[2] // want "index of nil slice"
284 }
285 for i := 0; i < len(slice); i++ {
286 _ = slice[i] // want "index of nil slice"
287 }
288 }
289
290 if array == nil {
291 // (The v var is necessary, otherwise the SSA
292 // code doesn't dereference the pointer.)
293 for _, v := range array { // want "nil dereference in array index operation"
294 _ = v
295 }
296 }
297
298 if m == nil {
299 for range m { // want "range over nil map"
300 }
301 m["one"] = 1 // want "nil dereference in map update"
302 }
303
304 if ch == nil {
305 for range ch { // want "receive from nil channel"
306 }
307 <-ch // want "receive from nil channel"
308 ch <- 0 // want "send to nil channel"
309 }
310 }
311
312 func f20() {
313 f, err := os.Open("")
314 if err != nil {
315 log.Fatal(err) // noreturn analysis proves this call doesn't return
316 }
317 if err != nil { // want "impossible condition"
318 log.Fatal(err)
319 }
320 f.Close()
321 }