Source file test/nilcheck.go

     1  // errorcheck -0 -N -d=nil
     2  
     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.
     6  
     7  // Test that nil checks are inserted.
     8  // Optimization is disabled, so redundant checks are not removed.
     9  
    10  package p
    11  
    12  type Struct struct {
    13  	X int
    14  	Y float64
    15  }
    16  
    17  type BigStruct struct {
    18  	X int
    19  	Y float64
    20  	A [1 << 20]int
    21  	Z string
    22  }
    23  
    24  type Empty struct {
    25  }
    26  
    27  type Empty1 struct {
    28  	Empty
    29  }
    30  
    31  var (
    32  	intp       *int
    33  	arrayp     *[10]int
    34  	array0p    *[0]int
    35  	bigarrayp  *[1 << 26]int
    36  	structp    *Struct
    37  	bigstructp *BigStruct
    38  	emptyp     *Empty
    39  	empty1p    *Empty1
    40  )
    41  
    42  func f1() {
    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"
    52  }
    53  
    54  func f2() {
    55  	var (
    56  		intp       *int
    57  		arrayp     *[10]int
    58  		array0p    *[0]int
    59  		bigarrayp  *[1 << 20]int
    60  		structp    *Struct
    61  		bigstructp *BigStruct
    62  		emptyp     *Empty
    63  		empty1p    *Empty1
    64  	)
    65  
    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"
    78  }
    79  
    80  func fx10k() *[10000]int
    81  
    82  var b bool
    83  
    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"
    88  
    89  	for {
    90  		if x[9999] != 0 { // ERROR "nil check"
    91  			break
    92  		}
    93  	}
    94  
    95  	x = fx10k()
    96  	_ = x[9999] // ERROR "nil check"
    97  	if b {
    98  		_ = x[9999] // ERROR "nil check"
    99  	} else {
   100  		_ = x[9999] // ERROR "nil check"
   101  	}
   102  	_ = x[9999] // ERROR "nil check"
   103  
   104  	x = fx10k()
   105  	if b {
   106  		_ = x[9999] // ERROR "nil check"
   107  	} else {
   108  		_ = x[9999] // ERROR "nil check"
   109  	}
   110  	_ = x[9999] // ERROR "nil check"
   111  
   112  	fx10k()
   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
   116  	// matter enough.
   117  	_ = x[9999] // ERROR "nil check"
   118  }
   119  
   120  func f3a() {
   121  	x := fx10k()
   122  	y := fx10k()
   123  	z := fx10k()
   124  	_ = &x[9] // ERROR "nil check"
   125  	y = z
   126  	_ = &x[9] // ERROR "nil check"
   127  	x = y
   128  	_ = &x[9] // ERROR "nil check"
   129  }
   130  
   131  func f3b() {
   132  	x := fx10k()
   133  	y := fx10k()
   134  	_ = &x[9] // ERROR "nil check"
   135  	y = x
   136  	_ = &x[9] // ERROR "nil check"
   137  	x = y
   138  	_ = &x[9] // ERROR "nil check"
   139  }
   140  
   141  func fx10() *[10]int
   142  
   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.
   147  
   148  	_ = x[9] // ERROR "nil check"
   149  
   150  	for {
   151  		if x[9] != 0 { // ERROR "nil check"
   152  			break
   153  		}
   154  	}
   155  
   156  	x = fx10()
   157  	_ = x[9] // ERROR "nil check"
   158  	if b {
   159  		_ = x[9] // ERROR "nil check"
   160  	} else {
   161  		_ = x[9] // ERROR "nil check"
   162  	}
   163  	_ = x[9] // ERROR "nil check"
   164  
   165  	x = fx10()
   166  	if b {
   167  		_ = x[9] // ERROR "nil check"
   168  	} else {
   169  		_ = &x[9] // ERROR "nil check"
   170  	}
   171  	_ = x[9] // ERROR "nil check"
   172  
   173  	fx10()
   174  	_ = x[9] // ERROR "nil check"
   175  
   176  	x = fx10()
   177  	y := fx10()
   178  	_ = &x[9] // ERROR "nil check"
   179  	y = x
   180  	_ = &x[9] // ERROR "nil check"
   181  	x = y
   182  	_ = &x[9] // ERROR "nil check"
   183  }
   184  
   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"
   188  	_, ok := tmp1, tmp2
   189  	return ok
   190  }
   191  

View as plain text