Skip to content

Commit 82317ce

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/analysis/modernize: slices.Delete: import slices
We forgot to add a call to AddImport: yet more evidence that our test framework needs to assert that fixes preserve well-typedness. RunWithSuggestedFix does a poor job of merging imports, so there are many duplicates in the golden file, but I will port the recent work in internal/checker to it. Change-Id: I976b52242772c2796b0cd54aab98d0710dbc2de9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647697 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent e65ea15 commit 82317ce

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

gopls/internal/analysis/modernize/slicesdelete.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"golang.org/x/tools/go/analysis"
1414
"golang.org/x/tools/go/analysis/passes/inspect"
1515
"golang.org/x/tools/go/ast/inspector"
16+
"golang.org/x/tools/internal/analysisinternal"
1617
)
1718

1819
// The slicesdelete pass attempts to replace instances of append(s[:i], s[i+k:]...)
@@ -22,20 +23,21 @@ import (
2223
func slicesdelete(pass *analysis.Pass) {
2324
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
2425
info := pass.TypesInfo
25-
report := func(call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) {
26+
report := func(file *ast.File, call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) {
27+
slicesName, edits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices")
2628
pass.Report(analysis.Diagnostic{
2729
Pos: call.Pos(),
2830
End: call.End(),
2931
Category: "slicesdelete",
3032
Message: "Replace append with slices.Delete",
3133
SuggestedFixes: []analysis.SuggestedFix{{
3234
Message: "Replace append with slices.Delete",
33-
TextEdits: []analysis.TextEdit{
35+
TextEdits: append(edits, []analysis.TextEdit{
3436
// Change name of called function.
3537
{
3638
Pos: call.Fun.Pos(),
3739
End: call.Fun.End(),
38-
NewText: []byte("slices.Delete"),
40+
NewText: []byte(slicesName + ".Delete"),
3941
},
4042
// Delete ellipsis.
4143
{
@@ -69,11 +71,12 @@ func slicesdelete(pass *analysis.Pass) {
6971
Pos: slice2.Low.End(),
7072
End: slice2.Rbrack + 1,
7173
},
72-
},
74+
}...),
7375
}},
7476
})
7577
}
7678
for curFile := range filesUsing(inspect, info, "go1.21") {
79+
file := curFile.Node().(*ast.File)
7780
for curCall := range curFile.Preorder((*ast.CallExpr)(nil)) {
7881
call := curCall.Node().(*ast.CallExpr)
7982
if id, ok := call.Fun.(*ast.Ident); ok && len(call.Args) == 2 {
@@ -88,7 +91,7 @@ func slicesdelete(pass *analysis.Pass) {
8891
equalSyntax(slice1.X, slice2.X) &&
8992
increasingSliceIndices(info, slice1.High, slice2.Low) {
9093
// Have append(s[:a], s[b:]...) where we can verify a < b.
91-
report(call, slice1, slice2)
94+
report(file, call, slice1, slice2)
9295
}
9396
}
9497
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package slicesdelete
22

3+
import "slices"
4+
5+
import "slices"
6+
7+
import "slices"
8+
9+
import "slices"
10+
11+
import "slices"
12+
13+
import "slices"
14+
15+
import "slices"
16+
17+
import "slices"
18+
319
var g struct{ f []int }
420

521
func slicesdelete(test, other []byte, i int) {

0 commit comments

Comments
 (0)