GoPLS Viewer

Home|gopls/go/analysis/passes/nilness/testdata/src/a/a.go
1package a
2
3type X struct{ fg int }
4
5func f(xy *X) {
6    if x == nil {
7        print(x.f// want "nil dereference in field selection"
8    } else {
9        print(x.f)
10    }
11
12    if x == nil {
13        if nil != y {
14            print(1)
15            panic(0)
16        }
17        x.f = 1 // want "nil dereference in field selection"
18        y.f = 1 // want "nil dereference in field selection"
19    }
20
21    var f func()
22    if f == nil { // want "tautological condition: nil == nil"
23        go f() // want "nil dereference in dynamic function call"
24    } else {
25        // This block is unreachable,
26        // so we don't report an error for the
27        // nil dereference in the call.
28        defer f()
29    }
30}
31
32func f2(ptr *[3]inti interface{}) {
33    if ptr != nil {
34        print(ptr[:])
35        *ptr = [3]int{}
36        print(*ptr)
37    } else {
38        print(ptr[:])   // want "nil dereference in slice operation"
39        *ptr = [3]int{} // want "nil dereference in store"
40        print(*ptr)     // want "nil dereference in load"
41
42        if ptr != nil { // want "impossible condition: nil != nil"
43            // Dominated by ptr==nil and ptr!=nil,
44            // this block is unreachable.
45            // We do not report errors within it.
46            print(*ptr)
47        }
48    }
49
50    if i != nil {
51        print(i.(interface{ f() }))
52    } else {
53        print(i.(interface{ f() })) // want "nil dereference in type assertion"
54    }
55}
56
57func g() error
58
59func f3() error {
60    err := g()
61    if err != nil {
62        return err
63    }
64    if err != nil && err.Error() == "foo" { // want "impossible condition: nil != nil"
65        print(0)
66    }
67    ch := make(chan int)
68    if ch == nil { // want "impossible condition: non-nil == nil"
69        print(0)
70    }
71    if ch != nil { // want "tautological condition: non-nil != nil"
72        print(0)
73    }
74    return nil
75}
76
77func h(err errorb bool) {
78    if err != nil && b {
79        return
80    } else if err != nil {
81        panic(err)
82    }
83}
84
85func i(*interror {
86    for {
87        if err := g(); err != nil {
88            return err
89        }
90    }
91}
92
93func f4(x *X) {
94    if x == nil {
95        panic(x)
96    }
97}
98
99func f5(x *X) {
100    panic(nil// want "panic with nil value"
101}
102
103func f6(x *X) {
104    var err error
105    panic(err// want "panic with nil value"
106}
107
108func f7() {
109    xerr := bad()
110    if err != nil {
111        panic(0)
112    }
113    if x == nil {
114        panic(err// want "panic with nil value"
115    }
116}
117
118func bad() (*Xerror) {
119    return nilnil
120}
121
122func f8() {
123    var e error
124    v_ := e.(interface{})
125    print(v)
126}
127
128func f9(x interface {
129    a()
130    b()
131    c()
132}) {
133    x.b() // we don't catch this panic because we don't have any facts yet
134    xx := interface {
135        a()
136        b()
137    }(x)
138    if xx != nil {
139        return
140    }
141    x.c()  // want "nil dereference in dynamic method call"
142    xx.b() // want "nil dereference in dynamic method call"
143    xxx := interface{ a() }(xx)
144    xxx.a() // want "nil dereference in dynamic method call"
145
146    if unknown() {
147        panic(x// want "panic with nil value"
148    }
149    if unknown() {
150        panic(xx// want "panic with nil value"
151    }
152    if unknown() {
153        panic(xxx// want "panic with nil value"
154    }
155}
156
157func f10() {
158    s0 := make([]string0)
159    if s0 == nil { // want "impossible condition: non-nil == nil"
160        print(0)
161    }
162
163    var s1 []string
164    if s1 == nil { // want "tautological condition: nil == nil"
165        print(0)
166    }
167    s2 := s1[:][:]
168    if s2 == nil { // want "tautological condition: nil == nil"
169        print(0)
170    }
171}
172
173func unknown() bool {
174    return false
175}
176
177func f11(a interface{}) {
178    switch a.(type) {
179    case nil:
180        return
181    }
182    switch a.(type) {
183    case nil// want "impossible condition: non-nil == nil"
184        return
185    }
186}
187
188func f12(a interface{}) {
189    switch a {
190    case nil:
191        return
192    }
193    switch a {
194    case 5,
195        nil// want "impossible condition: non-nil == nil"
196        return
197    }
198}
199
200type Y struct {
201    innerY
202}
203
204type innerY struct {
205    value int
206}
207
208func f13() {
209    var d *Y
210    print(d.value// want "nil dereference in field selection"
211}
212
213func f14() {
214    var x struct{ f string }
215    if x == struct{ f string }{} { // we don't catch this tautology as we restrict to reference types
216        print(x)
217    }
218}
219
MembersX
f5.x
f6.x
f13.d
f2.ptr
f2.i
f3
f4.x
f8.e
f12.a
f14
h.b
f6.err
f9.xx
f9.xxx
X.f
f4
f10
f11
Y
f9.x
f3.err
f3.ch
h
i.BlockStmt.err
f5
f6
f7.x
innerY
innerY.value
X
f.x
bad
f9
f10.s1
f11.a
f12
f.y
g
f7
f10.s0
unknown
f13
X.g
f
f2
h.err
i
f7.err
f8
Members
X