honnef.co/go/tools@v0.4.7/staticcheck/testdata/src/example.com/CheckMaybeNil/CheckMaybeNil.go (about)

     1  package pkg
     2  
     3  import (
     4  	"os"
     5  	"syscall"
     6  )
     7  
     8  func fn1(x *int) {
     9  	_ = *x //@ diag(`possible nil pointer dereference`)
    10  	if x != nil {
    11  		return
    12  	}
    13  	println()
    14  }
    15  
    16  func fn1_1(x *int) {
    17  	// this doesn't get flagged because the conditional return gets optimized away
    18  	_ = *x
    19  	if x != nil {
    20  		return
    21  	}
    22  }
    23  
    24  func fn2(x *int) {
    25  	if x == nil {
    26  		println("we should return")
    27  	}
    28  	_ = *x //@ diag(`possible nil pointer dereference`)
    29  }
    30  
    31  func fn3(x *int) {
    32  	if x != nil {
    33  		_ = *x
    34  	}
    35  }
    36  
    37  func fn4(x *int) {
    38  	if x == nil {
    39  		x = gen()
    40  	}
    41  	_ = *x
    42  }
    43  
    44  func fn5(x *int) {
    45  	if x == nil {
    46  		x = gen()
    47  	}
    48  	_ = *x //@ diag(`possible nil pointer dereference`)
    49  	if x == nil {
    50  		println("we should return")
    51  	}
    52  }
    53  
    54  func fn6() {
    55  	x := new(int)
    56  	if x == nil {
    57  		println("we should return")
    58  	}
    59  	// x can't be nil
    60  	_ = *x
    61  }
    62  
    63  func fn7() {
    64  	var x int
    65  	y := &x
    66  	if y == nil {
    67  		println("we should return")
    68  	}
    69  	// y can't be nil
    70  	_ = *y
    71  }
    72  
    73  func fn8(x *int) {
    74  	if x == nil {
    75  		return
    76  	}
    77  	// x can't be nil
    78  	_ = *x
    79  }
    80  
    81  func fn9(x *int) {
    82  	if x != nil {
    83  		return
    84  	}
    85  	// TODO(dh): not currently supported
    86  	_ = *x
    87  }
    88  
    89  func gen() *int { return nil }
    90  
    91  func die1(b bool) {
    92  	if b {
    93  		println("yay")
    94  		os.Exit(0)
    95  	} else {
    96  		println("nay")
    97  		os.Exit(1)
    98  	}
    99  }
   100  
   101  func die2(b bool) {
   102  	if b {
   103  		println("yay")
   104  		os.Exit(0)
   105  	}
   106  }
   107  
   108  func fn10(x *int) {
   109  	if x == nil {
   110  		die1(true)
   111  	}
   112  	_ = *x
   113  }
   114  
   115  func fn11(x *int) {
   116  	if x == nil {
   117  		die2(true)
   118  	}
   119  	_ = *x //@ diag(`possible nil pointer dereference`)
   120  }
   121  
   122  func doPanic() { panic("") }
   123  func doExit()  { syscall.Exit(1) }
   124  
   125  func fn12(arg bool) {
   126  	if arg {
   127  		doPanic()
   128  	} else {
   129  		doExit()
   130  	}
   131  }
   132  
   133  func fn13(arg bool) {
   134  	fn12(arg)
   135  }
   136  
   137  func fn14(x *int) {
   138  	if x == nil {
   139  		fn13(true)
   140  	}
   141  	_ = *x
   142  }
   143  
   144  func assert(b bool) {
   145  	if b {
   146  		panic("meh")
   147  	}
   148  }
   149  
   150  func fn15(x *int) {
   151  	assert(x != nil)
   152  	_ = *x
   153  }
   154  
   155  func fn16() {
   156  	var xs []int
   157  	if xs == nil {
   158  		println()
   159  	}
   160  
   161  	for _, x := range xs {
   162  		_ = x
   163  	}
   164  
   165  	var xs2 *[1]int
   166  	if xs2 == nil {
   167  		println()
   168  	}
   169  	// this used to get flagged, but now that we correctly insert sigma nodes for range loops, we can no longer flag this
   170  	for _, x := range xs2 {
   171  		_ = x
   172  	}
   173  
   174  	var xs3 *[]int
   175  	if xs3 == nil {
   176  		println()
   177  	}
   178  	for _, x := range *xs3 { //@ diag(`possible nil pointer dereference`)
   179  		_ = x
   180  	}
   181  
   182  	var xs4 []int
   183  	if xs4 == nil {
   184  		println()
   185  	}
   186  	_ = xs4[0]
   187  }