github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1001/testdata/src/example.com/CheckLoopCopy/copy_generics.go (about)

     1  //go:build go1.20
     2  
     3  package pkg
     4  
     5  func tpfn[T any]() {
     6  	var b1, b2 []T
     7  	for i, v := range b1 { //@ diag(`should use copy`)
     8  		b2[i] = v
     9  	}
    10  
    11  	for i := range b1 { //@ diag(`should use copy`)
    12  		b2[i] = b1[i]
    13  	}
    14  
    15  	type T2 [][16]T
    16  	var a T2
    17  	b := make([]any, len(a))
    18  	for i := range b {
    19  		b[i] = a[i]
    20  	}
    21  
    22  	var b3, b4 []*T
    23  	for i := range b3 { //@ diag(`should use copy`)
    24  		b4[i] = b3[i]
    25  	}
    26  
    27  	var m map[int]T
    28  	for i, v := range b1 {
    29  		m[i] = v
    30  	}
    31  
    32  }
    33  
    34  func tpsrc[T any]() []T { return nil }
    35  
    36  func tpfn1() {
    37  	// Don't flag this, the source is dynamic
    38  	var dst []any
    39  	for i := range tpsrc[any]() {
    40  		dst[i] = tpsrc[any]()[i]
    41  	}
    42  }
    43  
    44  func tpfn2[T any]() {
    45  	type T2 struct {
    46  		b []T
    47  	}
    48  
    49  	var src []T
    50  	var dst T2
    51  	for i, v := range src { //@ diag(`should use copy`)
    52  		dst.b[i] = v
    53  	}
    54  }
    55  
    56  func tpfn3[T any]() {
    57  	var src []T
    58  	var dst [][]T
    59  	for i, v := range src { //@ diag(`should use copy`)
    60  		dst[0][i] = v
    61  	}
    62  	for i, v := range src {
    63  		// Don't flag, destination depends on loop variable
    64  		dst[i][i] = v
    65  	}
    66  }
    67  
    68  func tpfn4[T any]() {
    69  	var b []T
    70  	var a1 [5]T
    71  	var a2 [10]T
    72  	var a3 [5]T
    73  
    74  	for i := range b { //@ diag(`should use copy`)
    75  		a1[i] = b[i]
    76  	}
    77  	for i := range a1 { //@ diag(`should use copy`)
    78  		b[i] = a1[i]
    79  	}
    80  	for i := range a1 { //@ diag(`should use copy`)
    81  		a2[i] = a1[i]
    82  	}
    83  	for i := range a1 { //@ diag(`should copy arrays using assignment`)
    84  		a3[i] = a1[i]
    85  	}
    86  
    87  	a1p := &a1
    88  	a2p := &a2
    89  	a3p := &a3
    90  	for i := range b { //@ diag(`should use copy`)
    91  		a1p[i] = b[i]
    92  	}
    93  	for i := range a1p { //@ diag(`should use copy`)
    94  		b[i] = a1p[i]
    95  	}
    96  	for i := range a1p { //@ diag(`should use copy`)
    97  		a2p[i] = a1p[i]
    98  	}
    99  	for i := range a1p { //@ diag(`should copy arrays using assignment`)
   100  		a3p[i] = a1p[i]
   101  	}
   102  
   103  	for i := range a1 { //@ diag(`should use copy`)
   104  		a2p[i] = a1[i]
   105  	}
   106  	for i := range a1 { //@ diag(`should copy arrays using assignment`)
   107  		a3p[i] = a1[i]
   108  	}
   109  	for i := range a1p { //@ diag(`should use copy`)
   110  		a2[i] = a1p[i]
   111  	}
   112  	for i := range a1p { //@ diag(`should copy arrays using assignment`)
   113  		a3[i] = a1p[i]
   114  	}
   115  }
   116  
   117  func tpfn5[T any]() {
   118  	var src, dst []T
   119  	for i := 0; i < len(src); i++ { //@ diag(`should use copy`)
   120  		dst[i] = src[i]
   121  	}
   122  
   123  	len := func([]T) int { return 0 }
   124  	for i := 0; i < len(src); i++ {
   125  		dst[i] = src[i]
   126  	}
   127  }