Skip to content

Commit 27453e4

Browse files
committed
fix: suggestion when m is not a variable
1 parent 115bd6d commit 27453e4

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

Diff for: exptostd.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,6 @@ func suggestedFixForClear(callExpr *ast.CallExpr) (analysis.SuggestedFix, error)
356356
}
357357

358358
func suggestedFixForKeysOrValues(callExpr *ast.CallExpr) (analysis.SuggestedFix, error) {
359-
var name string
360-
if ident, ok := callExpr.Args[0].(*ast.Ident); ok {
361-
name = ident.Name
362-
}
363-
364359
s := &ast.CallExpr{
365360
Fun: &ast.SelectorExpr{
366361
X: &ast.Ident{Name: "slices"},
@@ -375,10 +370,8 @@ func suggestedFixForKeysOrValues(callExpr *ast.CallExpr) (analysis.SuggestedFix,
375370
},
376371
&ast.BasicLit{Kind: token.INT, Value: "0"},
377372
&ast.CallExpr{
378-
Fun: &ast.Ident{Name: "len"},
379-
Args: []ast.Expr{
380-
&ast.Ident{Name: name},
381-
},
373+
Fun: &ast.Ident{Name: "len"},
374+
Args: callExpr.Args,
382375
},
383376
},
384377
},

Diff for: testdata/src/expmaps/maps.go

+24
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ func _(m, a map[string]string) {
2525

2626
maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`
2727
}
28+
29+
type foo struct {
30+
m map[string]int
31+
}
32+
33+
func _() {
34+
35+
f := foo{
36+
m: map[string]int{
37+
"foo": 1,
38+
"bar": 2,
39+
},
40+
}
41+
42+
maps.Keys(f.m) // want `golang.org/x/exp/maps\.Keys\(\) can be replaced by slices\.AppendSeq\(make\(\[\]T, 0, len\(data\)\), maps\.Keys\(data\)\)`
43+
}
44+
45+
func _() {
46+
maps.Keys(someFunc().m) // want `golang.org/x/exp/maps\.Keys\(\) can be replaced by slices\.AppendSeq\(make\(\[\]T, 0, len\(data\)\), maps\.Keys\(data\)\)`
47+
}
48+
49+
func someFunc() foo {
50+
return foo{}
51+
}

Diff for: testdata/src/expmaps/maps.go.golden

+23
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,26 @@ func _(m, a map[string]string) {
2626
clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`
2727
}
2828

29+
type foo struct {
30+
m map[string]int
31+
}
32+
33+
func _() {
34+
35+
f := foo{
36+
m: map[string]int{
37+
"foo": 1,
38+
"bar": 2,
39+
},
40+
}
41+
42+
slices.AppendSeq(make([]T, 0, len(f.m)), maps.Keys(f.m)) // want `golang.org/x/exp/maps\.Keys\(\) can be replaced by slices\.AppendSeq\(make\(\[\]T, 0, len\(data\)\), maps\.Keys\(data\)\)`
43+
}
44+
45+
func _() {
46+
slices.AppendSeq(make([]T, 0, len(someFunc().m)), maps.Keys(someFunc().m)) // want `golang.org/x/exp/maps\.Keys\(\) can be replaced by slices\.AppendSeq\(make\(\[\]T, 0, len\(data\)\), maps\.Keys\(data\)\)`
47+
}
48+
49+
func someFunc() foo {
50+
return foo{}
51+
}

0 commit comments

Comments
 (0)