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

     1  package pkg
     2  
     3  func fn() {
     4  	var b1, b2 []byte
     5  	copy(b2, b1)
     6  
     7  	copy(b2, b1)
     8  
     9  	type T [][16]byte
    10  	var a T
    11  	b := make([]interface{}, len(a))
    12  	for i := range b {
    13  		b[i] = a[i]
    14  	}
    15  
    16  	var b3, b4 []*byte
    17  	copy(b4, b3)
    18  
    19  	var m map[int]byte
    20  	for i, v := range b1 {
    21  		m[i] = v
    22  	}
    23  
    24  }
    25  
    26  func src() []interface{} { return nil }
    27  
    28  func fn1() {
    29  	// Don't flag this, the source is dynamic
    30  	var dst []interface{}
    31  	for i := range src() {
    32  		dst[i] = src()[i]
    33  	}
    34  }
    35  
    36  func fn2() {
    37  	type T struct {
    38  		b []byte
    39  	}
    40  
    41  	var src []byte
    42  	var dst T
    43  	copy(dst.b, src)
    44  }
    45  
    46  func fn3() {
    47  	var src []byte
    48  	var dst [][]byte
    49  	copy(dst[0], src)
    50  	for i, v := range src {
    51  		// Don't flag, destination depends on loop variable
    52  		dst[i][i] = v
    53  	}
    54  	for i, v := range src {
    55  		// Don't flag, destination depends on loop variable
    56  		dst[v][i] = v
    57  	}
    58  }
    59  
    60  func fn4() {
    61  	var b []byte
    62  	var a1 [5]byte
    63  	var a2 [10]byte
    64  	var a3 [5]byte
    65  
    66  	copy(a1[:], b)
    67  	copy(b, a1[:])
    68  	copy(a2[:], a1[:])
    69  	a3 = a1
    70  
    71  	a1p := &a1
    72  	a2p := &a2
    73  	a3p := &a3
    74  	copy(a1p[:], b)
    75  	copy(b, a1p[:])
    76  	copy(a2p[:], a1p[:])
    77  	*a3p = *a1p
    78  
    79  	copy(a2p[:], a1[:])
    80  	*a3p = a1
    81  	copy(a2[:], a1p[:])
    82  	a3 = *a1p
    83  }
    84  
    85  func fn5() {
    86  	var src, dst []byte
    87  	copy(dst, src)
    88  
    89  	len := func([]byte) int { return 0 }
    90  	for i := 0; i < len(src); i++ {
    91  		dst[i] = src[i]
    92  	}
    93  }
    94  
    95  func fn6() {
    96  	var src, dst []byte
    97  	copy := func() {}
    98  	_ = copy
    99  	for i, v := range src {
   100  		dst[i] = v
   101  	}
   102  }