blob: d27ff8c2a22575b9ef3227ab3317cccd364cb7c2 [file] [log] [blame]
Test of fixing redundant calls to maps.Keys and maps.Values
(both stdlib "maps" and "golang.org/x/exp/maps") for Go 1.21,
before range over int made suggesting a fix for a rare case easier.
-- go.mod --
module maprange
require golang.org/x/exp v0.0.0
replace golang.org/x/exp => ./exp
go 1.21
-- old.go --
package old
import "golang.org/x/exp/maps"
func _() {
m := make(map[int]int)
for i := range maps.Keys(m) { // want `likely incorrect use of golang.org/x/exp/maps.Keys \(returns a slice\)`
_ = i
}
}
-- old.go.golden --
package old
import "golang.org/x/exp/maps"
func _() {
m := make(map[int]int)
for i := range maps.Keys(m) { // want `likely incorrect use of golang.org/x/exp/maps.Keys \(returns a slice\)`
_ = i
}
}
-- exp/go.mod --
module golang.org/x/exp
go 1.21
-- exp/maps/maps.go --
package maps
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func Values[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, v := range m {
r = append(r, v)
}
return r
}