github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa4010/testdata/src/example.com/CheckIneffectiveAppend/CheckIneffectiveAppend.go (about)

     1  package pkg
     2  
     3  import "fmt"
     4  
     5  func fn1() {
     6  	var s []int
     7  	s = append(s, 1) //@ diag(`this result of append is never used`)
     8  	s = append(s, 1) //@ diag(`this result of append is never used`)
     9  }
    10  
    11  func fn2() (named []int) {
    12  	named = append(named, 1)
    13  	return
    14  }
    15  
    16  func fn3() {
    17  	s := make([]int, 0)
    18  	s = append(s, 1) //@ diag(`this result of append is never used`)
    19  }
    20  
    21  func fn3_1(n int) {
    22  	s := make([]int, n)
    23  	s = append(s, 1) //@ diag(`this result of append is never used`)
    24  }
    25  
    26  func fn4() []int {
    27  	var s []int
    28  	s = append(s, 1)
    29  	return s
    30  }
    31  
    32  func fn5() {
    33  	var s []int
    34  	s = append(s, 1)
    35  	fn6(s)
    36  }
    37  
    38  func fn6([]int) {}
    39  
    40  func fn7() {
    41  	var s []int
    42  	fn8(&s)
    43  	s = append(s, 1)
    44  }
    45  
    46  func fn8(*[]int) {}
    47  
    48  func fn9() {
    49  	var s []int
    50  	s = append(s, 1)
    51  	fmt.Println(s)
    52  	s = append(s, 1)
    53  }
    54  
    55  func fn10() {
    56  	var s []int
    57  	return
    58  	s = append(s, 1)
    59  }
    60  
    61  func fn11() {
    62  	var s []int
    63  	for x := 0; x < 10; x++ {
    64  		s = append(s, 1) //@ diag(`this result of append is never used`)
    65  	}
    66  }
    67  
    68  func fn12(a []int) {
    69  	b := a[:0]
    70  	for _, x := range a {
    71  		if true {
    72  			b = append(b, x)
    73  		}
    74  	}
    75  }
    76  
    77  func fn13() []byte {
    78  	a := make([]byte, 10)
    79  	b := a[:5]
    80  	for i := 0; i < 2; i++ {
    81  		a = append(a, 1)
    82  	}
    83  	return b
    84  }
    85  
    86  func fn14() []byte {
    87  	a := make([]byte, 10)
    88  	b := a[:5]
    89  	for i := 0; i < 2; i++ {
    90  		b = append(b, 1)
    91  	}
    92  	return a
    93  }
    94  
    95  func fn15() {
    96  	s := make([]byte, 0, 1)
    97  	retain(s)
    98  	s = append(s, 1)
    99  }
   100  
   101  func fn16(s []byte) {
   102  	for i := 0; i < 2; i++ {
   103  		s = append(s, 1)
   104  	}
   105  }
   106  
   107  func fn17(x *[5]byte) {
   108  	s := x[:0]
   109  	for i := 0; i < 2; i++ {
   110  		s = append(s, 1)
   111  	}
   112  }
   113  
   114  func fn18() {
   115  	var x [4]byte
   116  	s := x[:0]
   117  	for i := 0; i < 2; i++ {
   118  		s = append(s, 1)
   119  	}
   120  	_ = x
   121  }
   122  
   123  func fn19() [4]int {
   124  	var x [4]int
   125  	s := x[:]
   126  	s = append(s, 1)
   127  	return x
   128  }
   129  
   130  func fn20() {
   131  	var x [4]int
   132  	s := x[:]
   133  	s = append(s, 1) //@ diag(`this result of append is never used`)
   134  }
   135  
   136  func fn21() {
   137  	var x []byte
   138  	x = append(x, 1)
   139  	retain(x)
   140  	x = append(x, 2)
   141  }
   142  
   143  func fn22() {
   144  	// we should probably flag this, but we currently don't
   145  	var x1 []byte
   146  	x2 := append(x1, 1)
   147  	x2 = append(x2, 2)
   148  	x3 := append(x1, 3)
   149  	x3 = append(x3, 4)
   150  }
   151  
   152  func fn23(n int) []int {
   153  	s := make([]int, 0, n)
   154  	s2 := append(s, 1, 2, 3) // this can be observed by extending the capacity of x
   155  	s2 = append(s2, 4)
   156  	x := append(s, 2)
   157  	return x
   158  }
   159  
   160  func fn24() []byte {
   161  	x := make([]byte, 0, 24)
   162  	s1 := append(x, 1)
   163  	s2 := append(s1, 2)
   164  	s2 = append(s2, 3)
   165  	s3 := append(s1, 4)
   166  	return s3
   167  }
   168  
   169  func fn25() {
   170  	var s []byte
   171  	if true {
   172  		s = append(s, 1)
   173  	}
   174  	s = append(s, 2) //@ diag(`this result of append is never used`)
   175  }
   176  
   177  func fn26() {
   178  	var s []byte
   179  	if true {
   180  		s = append(s, 1)
   181  		retain(s)
   182  	}
   183  	s = append(s, 2)
   184  }
   185  
   186  func fn27() {
   187  	var s []byte
   188  	if true {
   189  		s = make([]byte, 0, 1)
   190  	} else {
   191  		s = make([]byte, 0, 2)
   192  	}
   193  	s = append(s, 1) //@ diag(`this result of append is never used`)
   194  }
   195  
   196  func fn28() {
   197  	var s []byte
   198  	if true {
   199  		s = make([]byte, 0, 1)
   200  	} else {
   201  		s = make([]byte, 0, 2)
   202  		retain(s)
   203  	}
   204  	s = append(s, 1)
   205  }
   206  
   207  func fn29() {
   208  	x := gen()
   209  	x = append(x, 1)
   210  }
   211  
   212  func fn30(x T) {
   213  	s := x.s
   214  	s = append(s, 1)
   215  }
   216  
   217  var Global []int
   218  
   219  func fn31() {
   220  	Global = append(Global, 1)
   221  }
   222  
   223  type T struct {
   224  	s []byte
   225  }
   226  
   227  func gen() []byte { return nil }
   228  
   229  func retain([]byte) {}