1 // errorcheck -0 -N -d=nil
3 // Copyright 2013 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test that nil checks are inserted.
8 // Optimization is disabled, so redundant checks are not removed.
17 type BigStruct struct {
35 bigarrayp *[1 << 26]int
43 _ = *intp // ERROR "nil check"
44 _ = *arrayp // ERROR "nil check"
45 _ = *array0p // ERROR "nil check"
46 _ = *array0p // ERROR "nil check"
47 _ = *intp // ERROR "nil check"
48 _ = *arrayp // ERROR "nil check"
49 _ = *structp // ERROR "nil check"
50 _ = *emptyp // ERROR "nil check"
51 _ = *arrayp // ERROR "nil check"
59 bigarrayp *[1 << 20]int
66 _ = *intp // ERROR "nil check"
67 _ = *arrayp // ERROR "nil check"
68 _ = *array0p // ERROR "nil check"
69 _ = *array0p // ERROR "nil check"
70 _ = *intp // ERROR "nil check"
71 _ = *arrayp // ERROR "nil check"
72 _ = *structp // ERROR "nil check"
73 _ = *emptyp // ERROR "nil check"
74 _ = *arrayp // ERROR "nil check"
75 _ = *bigarrayp // ERROR "nil check"
76 _ = *bigstructp // ERROR "nil check"
77 _ = *empty1p // ERROR "nil check"
80 func fx10k() *[10000]int
84 func f3(x *[10000]int) {
85 // Using a huge type and huge offsets so the compiler
86 // does not expect the memory hardware to fault.
87 _ = x[9999] // ERROR "nil check"
90 if x[9999] != 0 { // ERROR "nil check"
96 _ = x[9999] // ERROR "nil check"
98 _ = x[9999] // ERROR "nil check"
100 _ = x[9999] // ERROR "nil check"
102 _ = x[9999] // ERROR "nil check"
106 _ = x[9999] // ERROR "nil check"
108 _ = x[9999] // ERROR "nil check"
110 _ = x[9999] // ERROR "nil check"
113 // This one is a bit redundant, if we figured out that
114 // x wasn't going to change across the function call.
115 // But it's a little complex to do and in practice doesn't
117 _ = x[9999] // ERROR "nil check"
124 _ = &x[9] // ERROR "nil check"
126 _ = &x[9] // ERROR "nil check"
128 _ = &x[9] // ERROR "nil check"
134 _ = &x[9] // ERROR "nil check"
136 _ = &x[9] // ERROR "nil check"
138 _ = &x[9] // ERROR "nil check"
143 func f4(x *[10]int) {
144 // Most of these have no checks because a real memory reference follows,
145 // and the offset is small enough that if x is nil, the address will still be
146 // in the first unmapped page of memory.
148 _ = x[9] // ERROR "nil check"
151 if x[9] != 0 { // ERROR "nil check"
157 _ = x[9] // ERROR "nil check"
159 _ = x[9] // ERROR "nil check"
161 _ = x[9] // ERROR "nil check"
163 _ = x[9] // ERROR "nil check"
167 _ = x[9] // ERROR "nil check"
169 _ = &x[9] // ERROR "nil check"
171 _ = x[9] // ERROR "nil check"
174 _ = x[9] // ERROR "nil check"
178 _ = &x[9] // ERROR "nil check"
180 _ = &x[9] // ERROR "nil check"
182 _ = &x[9] // ERROR "nil check"
185 func f5(m map[string]struct{}) bool {
186 // Existence-only map lookups should not generate a nil check
187 tmp1, tmp2 := m[""] // ERROR "removed nil check"