| 1 |
//go:build go1.23 |
| 2 |
|
| 3 |
package mapsloop |
| 4 |
|
| 5 |
import ( |
| 6 |
"iter" |
| 7 |
"maps" |
| 8 |
) |
| 9 |
|
| 10 |
var _ = maps.Clone[M] // force "maps" import so that each diagnostic doesn't add one |
| 11 |
|
| 12 |
type M map[int]string |
| 13 |
|
| 14 |
// -- src is map -- |
| 15 |
|
| 16 |
func useCopy(dst, src map[int]string) { |
| 17 |
// Replace loop by maps.Copy. |
| 18 |
// A |
| 19 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 20 |
maps.Copy(dst, src) |
| 21 |
} |
| 22 |
|
| 23 |
func useCopyRetrieveMap(x map[int]int) { |
| 24 |
getMap := func(int) map[int]int { return nil } |
| 25 |
// TODO(yuchen): don't assume that getMap returns the same map each time and has no effects. |
| 26 |
// |
| 27 |
// So, to avoid changing the cardinality of side effects, |
| 28 |
// the limit expression must not involve function calls (e.g. seq.Len()) or channel receives. |
| 29 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 30 |
maps.Copy(getMap(0), x) |
| 31 |
} |
| 32 |
|
| 33 |
func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { |
| 34 |
// Replace loop by maps.Copy. |
| 35 |
// A |
| 36 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 37 |
maps.Copy(dst, src) |
| 38 |
} |
| 39 |
|
| 40 |
func useCopyNotClone(src map[int]string) { |
| 41 |
// Clone is tempting but wrong when src may be nil; see #71844. |
| 42 |
|
| 43 |
// Replace make(...) by maps.Copy. |
| 44 |
dst := make(map[int]string, len(src)) |
| 45 |
// A |
| 46 |
// B |
| 47 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 48 |
// C |
| 49 |
maps.Copy(dst, src) |
| 50 |
|
| 51 |
// A |
| 52 |
dst = map[int]string{} |
| 53 |
// B |
| 54 |
// C |
| 55 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 56 |
maps.Copy(dst, src) |
| 57 |
println(dst) |
| 58 |
} |
| 59 |
|
| 60 |
func useCopyParen(src map[int]string) { |
| 61 |
// Clone is tempting but wrong when src may be nil; see #71844. |
| 62 |
|
| 63 |
// Replace (make)(...) by maps.Clone. |
| 64 |
dst := (make)(map[int]string, len(src)) |
| 65 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 66 |
maps.Copy(dst, src) |
| 67 |
|
| 68 |
dst = (map[int]string{}) |
| 69 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 70 |
maps.Copy(dst, src) |
| 71 |
println(dst) |
| 72 |
} |
| 73 |
|
| 74 |
func useCopy_typesDiffer(src M) { |
| 75 |
// Replace loop but not make(...) as maps.Copy(src) would return wrong type M. |
| 76 |
dst := make(map[int]string, len(src)) |
| 77 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 78 |
maps.Copy(dst, src) |
| 79 |
println(dst) |
| 80 |
} |
| 81 |
|
| 82 |
func useCopy_typesDiffer2(src map[int]string) { |
| 83 |
// Replace loop but not make(...) as maps.Copy(src) would return wrong type map[int]string. |
| 84 |
dst := make(M, len(src)) |
| 85 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 86 |
maps.Copy(dst, src) |
| 87 |
println(dst) |
| 88 |
} |
| 89 |
|
| 90 |
func useClone_typesDiffer3(src map[int]string) { |
| 91 |
// Clone is tempting but wrong when src may be nil; see #71844. |
| 92 |
|
| 93 |
// Replace loop and make(...) as maps.Clone(src) returns map[int]string |
| 94 |
// which is assignable to M. |
| 95 |
var dst M |
| 96 |
dst = make(M, len(src)) |
| 97 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 98 |
maps.Copy(dst, src) |
| 99 |
println(dst) |
| 100 |
} |
| 101 |
|
| 102 |
func useClone_typesDiffer4(src map[int]string) { |
| 103 |
// Clone is tempting but wrong when src may be nil; see #71844. |
| 104 |
|
| 105 |
// Replace loop and make(...) as maps.Clone(src) returns map[int]string |
| 106 |
// which is assignable to M. |
| 107 |
var dst M |
| 108 |
dst = make(M, len(src)) |
| 109 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 110 |
maps.Copy(dst, src) |
| 111 |
println(dst) |
| 112 |
} |
| 113 |
|
| 114 |
func useClone_generic[Map ~map[K]V, K comparable, V any](src Map) { |
| 115 |
// Clone is tempting but wrong when src may be nil; see #71844. |
| 116 |
|
| 117 |
// Replace loop and make(...) by maps.Clone |
| 118 |
dst := make(Map, len(src)) |
| 119 |
// want "Replace m\\[k\\]=v loop with maps.Copy" |
| 120 |
maps.Copy(dst, src) |
| 121 |
println(dst) |
| 122 |
} |
| 123 |
|
| 124 |
// -- src is iter.Seq2 -- |
| 125 |
|
| 126 |
func useInsert_assignableToSeq2(dst map[int]string, src func(yield func(int, string) bool)) { |
| 127 |
// Replace loop by maps.Insert because src is assignable to iter.Seq2. |
| 128 |
// want "Replace m\\[k\\]=v loop with maps.Insert" |
| 129 |
maps.Insert(dst, src) |
| 130 |
} |
| 131 |
|
| 132 |
func useCollect(src iter.Seq2[int, string]) { |
| 133 |
// Replace loop and make(...) by maps.Collect. |
| 134 |
var dst map[int]string |
| 135 |
// A |
| 136 |
// B |
| 137 |
// C |
| 138 |
// want "Replace m\\[k\\]=v loop with maps.Collect" |
| 139 |
dst = maps.Collect(src) |
| 140 |
} |
| 141 |
|
| 142 |
func useInsert_typesDifferAssign(src iter.Seq2[int, string]) { |
| 143 |
// Replace loop and make(...): maps.Collect returns an unnamed map type |
| 144 |
// that is assignable to M. |
| 145 |
var dst M |
| 146 |
// A |
| 147 |
// B |
| 148 |
// want "Replace m\\[k\\]=v loop with maps.Collect" |
| 149 |
dst = maps.Collect(src) |
| 150 |
} |
| 151 |
|
| 152 |
func useInsert_typesDifferDeclare(src iter.Seq2[int, string]) { |
| 153 |
// Replace loop but not make(...) as maps.Collect would return an |
| 154 |
// unnamed map type that would change the type of dst. |
| 155 |
dst := make(M) |
| 156 |
// want "Replace m\\[k\\]=v loop with maps.Insert" |
| 157 |
maps.Insert(dst, src) |
| 158 |
} |
| 159 |
|
| 160 |
// Regression test for go.dev/issues/78322 |
| 161 |
func useCollect_mapDefine(it iter.Seq2[string, int]) map[string]int { |
| 162 |
// want "Replace m\\[k\\]=v loop with maps.Collect" |
| 163 |
m := maps.Collect(it) |
| 164 |
return m |
| 165 |
} |
| 166 |
|
| 167 |
// -- non-matches -- |
| 168 |
|
| 169 |
type isomerOfSeq2 func(yield func(int, string) bool) |
| 170 |
|
| 171 |
func nopeInsertRequiresAssignableToSeq2(dst map[int]string, src isomerOfSeq2) { |
| 172 |
for k, v := range src { // nope: src is not assignable to maps.Insert's iter.Seq2 parameter |
| 173 |
dst[k] = v |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func nopeSingleVarRange(dst map[int]bool, src map[int]string) { |
| 178 |
for key := range src { // nope: must be "for k, v" |
| 179 |
dst[key] = true |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func nopeBodyNotASingleton(src map[int]string) { |
| 184 |
var dst map[int]string |
| 185 |
for key, value := range src { |
| 186 |
dst[key] = value |
| 187 |
println() // nope: other things in the loop body |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Regression test for https://github.com/golang/go/issues/70815#issuecomment-2581999787. |
| 192 |
func nopeAssignmentHasIncrementOperator(src map[int]int) { |
| 193 |
dst := make(map[int]int) |
| 194 |
for k, v := range src { |
| 195 |
dst[k] += v |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func nopeNotAMap(src map[int]string) { |
| 200 |
var dst []string |
| 201 |
for k, v := range src { |
| 202 |
dst[k] = v |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func nopeNotAMapGeneric[E any, M ~map[int]E, S ~[]E](src M) { |
| 207 |
var dst S |
| 208 |
for k, v := range src { |
| 209 |
dst[k] = v |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func nopeHasImplicitValueWidening(src map[string]int) { |
| 214 |
dst := make(map[string]any) |
| 215 |
for k, v := range src { |
| 216 |
dst[k] = v |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func nopeHasImplicitKeyWidening(src map[string]string) { |
| 221 |
dst := make(map[any]string) |
| 222 |
for k, v := range src { |
| 223 |
dst[k] = v |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// The expression for the map (y[v]) must not itself refer to loop variables. |
| 228 |
// See https://go.dev/issue/77008. |
| 229 |
func nopeMapExprUsesLoopVars(x map[int]int, y []map[int]int) { |
| 230 |
for i, v := range x { |
| 231 |
y[v][i] = v |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func nopeExtraKeyValueUsage(x map[int]int, y []map[int]int) { |
| 236 |
for i, v := range x { |
| 237 |
y[i][i] = v |
| 238 |
} |
| 239 |
|
| 240 |
getMap := func(int) map[int]int { return nil } |
| 241 |
for i, v := range x { |
| 242 |
getMap(i)[i] = v |
| 243 |
} |
| 244 |
|
| 245 |
for i, v := range x { |
| 246 |
getMap(v)[i] = v |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func nope2LhsAssigment(x map[int]int, y map[int]int) { |
| 251 |
for i, v := range x { |
| 252 |
y[i], _ = v, 0 |
| 253 |
} |
| 254 |
} |