Skip to content

Commit ead62e9

Browse files
committed
gopls/internal/analysis/modernize: handle parens
In the maps modernizer, consider the possibility of parentheses surrounding some bits of syntax. Change-Id: I395de81b99f2e9b47dca7f4bbfbed66c0772b6f6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649975 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 32ffaa3 commit ead62e9

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

gopls/internal/analysis/modernize/maps.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func mapsloop(pass *analysis.Pass) {
8787
// Have: m = rhs; for k, v := range x { m[k] = v }
8888
var newMap bool
8989
rhs := assign.Rhs[0]
90-
switch rhs := rhs.(type) {
90+
switch rhs := ast.Unparen(rhs).(type) {
9191
case *ast.CallExpr:
92-
if id, ok := rhs.Fun.(*ast.Ident); ok &&
92+
if id, ok := ast.Unparen(rhs.Fun).(*ast.Ident); ok &&
9393
info.Uses[id] == builtinMake {
9494
// Have: m = make(...)
9595
newMap = true

gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ func useClone(src map[int]string) {
3333
for key, value := range src {
3434
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone"
3535
}
36+
37+
dst = map[int]string{}
38+
for key, value := range src {
39+
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone"
40+
}
41+
println(dst)
42+
}
43+
44+
func useCloneParen(src map[int]string) {
45+
// Replace (make)(...) by maps.Clone.
46+
dst := (make)(map[int]string, len(src))
47+
for key, value := range src {
48+
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone"
49+
}
50+
51+
dst = (map[int]string{})
52+
for key, value := range src {
53+
dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone"
54+
}
3655
println(dst)
3756
}
3857

gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) {
2626
func useClone(src map[int]string) {
2727
// Replace make(...) by maps.Clone.
2828
dst := maps.Clone(src)
29+
30+
dst = maps.Clone(src)
31+
println(dst)
32+
}
33+
34+
func useCloneParen(src map[int]string) {
35+
// Replace (make)(...) by maps.Clone.
36+
dst := maps.Clone(src)
37+
38+
dst = maps.Clone(src)
2939
println(dst)
3040
}
3141

0 commit comments

Comments
 (0)