Debian Codegraph @

go/analysis/passes/modernize/testdata/src/mapsloop/mapsloop.go

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