github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1011/testdata/src/example.com/CheckLoopAppend/loop-append.go (about)

     1  package pkg
     2  
     3  type T struct {
     4  	F string
     5  }
     6  
     7  func fn1() {
     8  	var x []interface{}
     9  	var y []int
    10  
    11  	for _, v := range y {
    12  		x = append(x, v)
    13  	}
    14  
    15  	var a, b []int
    16  	for _, v := range a { //@ diag(`should replace loop`)
    17  		b = append(b, v)
    18  	}
    19  
    20  	var a2, b2 []int
    21  	for i := range a2 { //@ diag(`should replace loop`)
    22  		b2 = append(b2, a2[i])
    23  	}
    24  
    25  	var a3, b3 []int
    26  	for i := range a3 { //@ diag(`should replace loop`)
    27  		v := a3[i]
    28  		b3 = append(b3, v)
    29  	}
    30  
    31  	var a4 []int
    32  	for i := range fn6() {
    33  		a4 = append(a4, fn6()[i])
    34  	}
    35  
    36  	var m map[string]int
    37  	var c []int
    38  	for _, v := range m {
    39  		c = append(c, v)
    40  	}
    41  
    42  	var t []T
    43  	var m2 map[string][]T
    44  
    45  	for _, tt := range t {
    46  		m2[tt.F] = append(m2[tt.F], tt)
    47  	}
    48  
    49  	var out []T
    50  	for _, tt := range t {
    51  		out = append(m2[tt.F], tt)
    52  	}
    53  	_ = out
    54  }
    55  
    56  func fn2() {
    57  	var v struct {
    58  		V int
    59  	}
    60  	var in []int
    61  	var out []int
    62  
    63  	for _, v.V = range in {
    64  		out = append(out, v.V)
    65  	}
    66  }
    67  
    68  func fn3() {
    69  	var t []T
    70  	var out [][]T
    71  	var m2 map[string][]T
    72  
    73  	for _, tt := range t {
    74  		out = append(out, m2[tt.F])
    75  	}
    76  }
    77  
    78  func fn4() {
    79  	var a, b, c []int
    80  	for _, v := range a {
    81  		b = append(c, v)
    82  	}
    83  	_ = b
    84  }
    85  
    86  func fn5() {
    87  	var t []T
    88  	var m2 map[string][]T
    89  	var out []T
    90  	for _, tt := range t {
    91  		out = append(m2[tt.F], tt)
    92  	}
    93  	_ = out
    94  }
    95  
    96  func fn6() []int {
    97  	return []int{1, 2, 3}
    98  }
    99  
   100  func fn7() {
   101  	var x []int
   102  	for _, v := range fn6() { //@ diag(`should replace loop`)
   103  		// Purity doesn't matter here
   104  		x = append(x, v)
   105  	}
   106  
   107  	for i := range fn6() {
   108  		// Purity does matter here
   109  		x = append(x, fn6()[i])
   110  	}
   111  }
   112  
   113  func fn8() {
   114  	// The lhs isn't allowed to refer to i
   115  	var i int
   116  	var x []int
   117  	var y [][]int
   118  	for i = range x {
   119  		y[i] = append(y[i], x[i])
   120  	}
   121  	for i := range x {
   122  		y[i] = append(y[i], x[i])
   123  	}
   124  }
   125  
   126  func fn9() {
   127  	// The lhs isn't allowed to have side effects
   128  	bar := func() map[int][]int { return nil }
   129  	var x []int
   130  	for i := range x {
   131  		bar()[0] = append(bar()[0], x[i])
   132  	}
   133  }