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