.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / staticcheck / testdata / src / CheckMaybeNil / CheckMaybeNil.go
1 package pkg
2
3 import (
4         "os"
5         "syscall"
6 )
7
8 func fn1(x *int) {
9         _ = *x // want `possible nil pointer dereference`
10         if x != nil {
11                 return
12         }
13 }
14
15 func fn2(x *int) {
16         if x == nil {
17                 println("we should return")
18         }
19         _ = *x // want `possible nil pointer dereference`
20 }
21
22 func fn3(x *int) {
23         if x != nil {
24                 _ = *x
25         }
26 }
27
28 func fn4(x *int) {
29         if x == nil {
30                 x = gen()
31         }
32         _ = *x
33 }
34
35 func fn5(x *int) {
36         if x == nil {
37                 x = gen()
38         }
39         _ = *x // want `possible nil pointer dereference`
40         if x == nil {
41                 println("we should return")
42         }
43 }
44
45 func fn6() {
46         x := new(int)
47         if x == nil {
48                 println("we should return")
49         }
50         // x can't be nil
51         _ = *x
52 }
53
54 func fn7() {
55         var x int
56         y := &x
57         if y == nil {
58                 println("we should return")
59         }
60         // y can't be nil
61         _ = *y
62 }
63
64 func fn8(x *int) {
65         if x == nil {
66                 return
67         }
68         // x can't be nil
69         _ = *x
70 }
71
72 func fn9(x *int) {
73         if x != nil {
74                 return
75         }
76         // TODO(dh): not currently supported
77         _ = *x
78 }
79
80 func gen() *int { return nil }
81
82 func die1(b bool) {
83         if b {
84                 println("yay")
85                 os.Exit(0)
86         } else {
87                 println("nay")
88                 os.Exit(1)
89         }
90 }
91
92 func die2(b bool) {
93         if b {
94                 println("yay")
95                 os.Exit(0)
96         }
97 }
98
99 func fn10(x *int) {
100         if x == nil {
101                 die1(true)
102         }
103         _ = *x
104 }
105
106 func fn11(x *int) {
107         if x == nil {
108                 die2(true)
109         }
110         _ = *x // want `possible nil pointer dereference`
111 }
112
113 func doPanic() { panic("") }
114 func doExit()  { syscall.Exit(1) }
115
116 func fn12(arg bool) {
117         if arg {
118                 doPanic()
119         } else {
120                 doExit()
121         }
122 }
123
124 func fn13(arg bool) {
125         fn12(arg)
126 }
127
128 func fn14(x *int) {
129         if x == nil {
130                 fn13(true)
131         }
132         _ = *x
133 }