2013-08-15 12:38:32 -06:00
|
|
|
// errorcheck -0 -N -d=nil
|
|
|
|
|
2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
2013-08-15 12:38:32 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Test that nil checks are inserted.
|
|
|
|
// Optimization is disabled, so redundant checks are not removed.
|
|
|
|
|
|
|
|
package p
|
|
|
|
|
|
|
|
type Struct struct {
|
|
|
|
X int
|
|
|
|
Y float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type BigStruct struct {
|
|
|
|
X int
|
|
|
|
Y float64
|
2015-10-26 15:34:06 -06:00
|
|
|
A [1 << 20]int
|
2013-08-15 12:38:32 -06:00
|
|
|
Z string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Empty struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type Empty1 struct {
|
|
|
|
Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-10-26 15:34:06 -06:00
|
|
|
intp *int
|
|
|
|
arrayp *[10]int
|
|
|
|
array0p *[0]int
|
|
|
|
bigarrayp *[1 << 26]int
|
|
|
|
structp *Struct
|
2013-08-15 12:38:32 -06:00
|
|
|
bigstructp *BigStruct
|
2015-10-26 15:34:06 -06:00
|
|
|
emptyp *Empty
|
|
|
|
empty1p *Empty1
|
2013-08-15 12:38:32 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func f1() {
|
2015-10-26 15:34:06 -06:00
|
|
|
_ = *intp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = *array0p // ERROR "nil check"
|
|
|
|
_ = *array0p // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
_ = *intp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = *structp // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
_ = *emptyp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
2013-08-15 12:38:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func f2() {
|
|
|
|
var (
|
2015-10-26 15:34:06 -06:00
|
|
|
intp *int
|
|
|
|
arrayp *[10]int
|
|
|
|
array0p *[0]int
|
|
|
|
bigarrayp *[1 << 20]int
|
|
|
|
structp *Struct
|
2013-08-15 12:38:32 -06:00
|
|
|
bigstructp *BigStruct
|
2015-10-26 15:34:06 -06:00
|
|
|
emptyp *Empty
|
|
|
|
empty1p *Empty1
|
2013-08-15 12:38:32 -06:00
|
|
|
)
|
|
|
|
|
2015-10-26 15:34:06 -06:00
|
|
|
_ = *intp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
|
|
|
_ = *array0p // ERROR "nil check"
|
|
|
|
_ = *array0p // ERROR "nil check"
|
|
|
|
_ = *intp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
|
|
|
_ = *structp // ERROR "nil check"
|
|
|
|
_ = *emptyp // ERROR "nil check"
|
|
|
|
_ = *arrayp // ERROR "nil check"
|
|
|
|
_ = *bigarrayp // ERROR "nil check"
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = *bigstructp // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
_ = *empty1p // ERROR "nil check"
|
2013-08-15 12:38:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func fx10k() *[10000]int
|
|
|
|
|
2015-10-26 15:34:06 -06:00
|
|
|
var b bool
|
2013-08-15 12:38:32 -06:00
|
|
|
|
|
|
|
func f3(x *[10000]int) {
|
|
|
|
// Using a huge type and huge offsets so the compiler
|
|
|
|
// does not expect the memory hardware to fault.
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
|
2013-08-15 12:38:32 -06:00
|
|
|
for {
|
|
|
|
if x[9999] != 0 { // ERROR "nil check"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 15:34:06 -06:00
|
|
|
|
|
|
|
x = fx10k()
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9999] // ERROR "nil check"
|
|
|
|
if b {
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
|
|
|
} else {
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
}
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9999] // ERROR "nil check"
|
|
|
|
|
2015-10-26 15:34:06 -06:00
|
|
|
x = fx10k()
|
2013-08-15 12:38:32 -06:00
|
|
|
if b {
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
|
|
|
} else {
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
}
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9999] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
|
2013-08-15 12:38:32 -06:00
|
|
|
fx10k()
|
|
|
|
// This one is a bit redundant, if we figured out that
|
|
|
|
// x wasn't going to change across the function call.
|
|
|
|
// But it's a little complex to do and in practice doesn't
|
|
|
|
// matter enough.
|
|
|
|
_ = x[9999] // ERROR "nil check"
|
|
|
|
}
|
|
|
|
|
|
|
|
func f3a() {
|
|
|
|
x := fx10k()
|
|
|
|
y := fx10k()
|
|
|
|
z := fx10k()
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
y = z
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
x = y
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
}
|
|
|
|
|
|
|
|
func f3b() {
|
|
|
|
x := fx10k()
|
|
|
|
y := fx10k()
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
y = x
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
x = y
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:34:06 -06:00
|
|
|
func fx10() *[10]int
|
2013-08-15 12:38:32 -06:00
|
|
|
|
|
|
|
func f4(x *[10]int) {
|
|
|
|
// Most of these have no checks because a real memory reference follows,
|
|
|
|
// and the offset is small enough that if x is nil, the address will still be
|
|
|
|
// in the first unmapped page of memory.
|
|
|
|
|
|
|
|
_ = x[9] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
|
2013-08-15 12:38:32 -06:00
|
|
|
for {
|
|
|
|
if x[9] != 0 { // ERROR "nil check"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 15:34:06 -06:00
|
|
|
|
|
|
|
x = fx10()
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9] // ERROR "nil check"
|
|
|
|
if b {
|
|
|
|
_ = x[9] // ERROR "nil check"
|
|
|
|
} else {
|
|
|
|
_ = x[9] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
}
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9] // ERROR "nil check"
|
|
|
|
|
2015-10-26 15:34:06 -06:00
|
|
|
x = fx10()
|
2013-08-15 12:38:32 -06:00
|
|
|
if b {
|
|
|
|
_ = x[9] // ERROR "nil check"
|
|
|
|
} else {
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
}
|
2013-08-15 12:38:32 -06:00
|
|
|
_ = x[9] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
|
2013-08-15 12:38:32 -06:00
|
|
|
fx10()
|
|
|
|
_ = x[9] // ERROR "nil check"
|
2015-10-26 15:34:06 -06:00
|
|
|
|
2013-08-15 12:38:32 -06:00
|
|
|
x = fx10()
|
|
|
|
y := fx10()
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
y = x
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
x = y
|
|
|
|
_ = &x[9] // ERROR "nil check"
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:33:47 -07:00
|
|
|
func f5(m map[string]struct{}) bool {
|
|
|
|
// Existence-only map lookups should not generate a nil check
|
|
|
|
_, ok := m[""]
|
|
|
|
return ok
|
|
|
|
}
|