Debian Codegraph @

gopls/internal/analysis/maprange/testdata/basic.txtar

raw
1 Test of fixing redundant calls to maps.Keys and maps.Values
2 (both stdlib "maps" and "golang.org/x/exp/maps") for Go 1.24.
3
4 -- go.mod --
5 module maprange
6
7 require golang.org/x/exp v0.0.0
8
9 replace golang.org/x/exp => ./exp
10
11 go 1.24
12
13 -- basic.go --
14 package basic
15
16 import "maps"
17
18 func _() {
19 m := make(map[int]int)
20
21 for range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys`
22 }
23
24 for range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values`
25 }
26
27 var x struct {
28 Map map[int]int
29 }
30 x.Map = make(map[int]int)
31 for x.Map[1] = range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys`
32 }
33
34 for x.Map[2] = range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values`
35 }
36
37 for k := range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys`
38 _ = k
39 }
40
41 for v := range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values`
42 _ = v
43 }
44
45 for range maps.Keys(x.Map) { // want `unnecessary and inefficient call of maps.Keys`
46 }
47
48 for /* comment */ k := range /* comment */ maps.Keys(/* comment */ m) { // want `unnecessary and inefficient call of maps.Keys`
49 _ = k
50 }
51 }
52
53 -- basic.go.golden --
54 package basic
55
56 func _() {
57 m := make(map[int]int)
58
59 for range m { // want `unnecessary and inefficient call of maps.Keys`
60 }
61
62 for range m { // want `unnecessary and inefficient call of maps.Values`
63 }
64
65 var x struct {
66 Map map[int]int
67 }
68 x.Map = make(map[int]int)
69 for x.Map[1] = range m { // want `unnecessary and inefficient call of maps.Keys`
70 }
71
72 for _, x.Map[2] = range m { // want `unnecessary and inefficient call of maps.Values`
73 }
74
75 for k := range m { // want `unnecessary and inefficient call of maps.Keys`
76 _ = k
77 }
78
79 for _, v := range m { // want `unnecessary and inefficient call of maps.Values`
80 _ = v
81 }
82
83 for range x.Map { // want `unnecessary and inefficient call of maps.Keys`
84 }
85
86 for /* comment */ k := range /* comment */ /* comment */ m { // want `unnecessary and inefficient call of maps.Keys`
87 _ = k
88 }
89 }
90
91 -- xmaps.go --
92 package basic
93
94 import "golang.org/x/exp/maps"
95
96 func _() {
97 m := make(map[int]int)
98
99 for range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
100 }
101
102 for range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
103 }
104
105 for i := range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
106 _ = i
107 }
108
109 var x struct {
110 Map map[int]int
111 }
112 x.Map = make(map[int]int)
113 for _, x.Map[1] = range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
114 }
115
116 for _, x.Map[2] = range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
117 }
118
119 for _, k := range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
120 _ = k
121 }
122
123 for _, v := range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
124 _ = v
125 }
126
127 for range maps.Keys(x.Map) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
128 }
129
130 for i, k := range maps.Keys(m) { // ok: this can't be straightforwardly rewritten
131 _, _ = i, k
132 }
133
134 for _, _ = range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
135 }
136 }
137
138 -- xmaps.go.golden --
139 package basic
140
141 import "golang.org/x/exp/maps"
142
143 func _() {
144 m := make(map[int]int)
145
146 for range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
147 }
148
149 for range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
150 }
151
152 for i := range len(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
153 _ = i
154 }
155
156 var x struct {
157 Map map[int]int
158 }
159 x.Map = make(map[int]int)
160 for x.Map[1] = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
161 }
162
163 for _, x.Map[2] = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
164 }
165
166 for k := range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
167 _ = k
168 }
169
170 for _, v := range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
171 _ = v
172 }
173
174 for range x.Map { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys`
175 }
176
177 for i, k := range maps.Keys(m) { // ok: this can't be straightforwardly rewritten
178 _, _ = i, k
179 }
180
181 for _, _ = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values`
182 }
183 }
184
185 -- exp/go.mod --
186 module golang.org/x/exp
187
188 go 1.24
189
190 -- exp/maps/maps.go --
191 package maps
192
193 func Keys[M ~map[K]V, K comparable, V any](m M) []K {
194 r := make([]K, 0, len(m))
195 for k := range m {
196 r = append(r, k)
197 }
198 return r
199 }
200
201 func Values[M ~map[K]V, K comparable, V any](m M) []V {
202 r := make([]V, 0, len(m))
203 for _, v := range m {
204 r = append(r, v)
205 }
206 return r
207 }