Skip to content

Commit

Permalink
gopls/internal/analysis/modernize: slices.Delete: import slices
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
adonovan authored and gopherbot committed Feb 7, 2025
1 parent e65ea15 commit 82317ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 8 additions & 5 deletions gopls/internal/analysis/modernize/slicesdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/analysisinternal"
)

// The slicesdelete pass attempts to replace instances of append(s[:i], s[i+k:]...)
Expand All @@ -22,20 +23,21 @@ import (
func slicesdelete(pass *analysis.Pass) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
info := pass.TypesInfo
report := func(call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) {
report := func(file *ast.File, call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) {
slicesName, edits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices")
pass.Report(analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Category: "slicesdelete",
Message: "Replace append with slices.Delete",
SuggestedFixes: []analysis.SuggestedFix{{
Message: "Replace append with slices.Delete",
TextEdits: []analysis.TextEdit{
TextEdits: append(edits, []analysis.TextEdit{
// Change name of called function.
{
Pos: call.Fun.Pos(),
End: call.Fun.End(),
NewText: []byte("slices.Delete"),
NewText: []byte(slicesName + ".Delete"),
},
// Delete ellipsis.
{
Expand Down Expand Up @@ -69,11 +71,12 @@ func slicesdelete(pass *analysis.Pass) {
Pos: slice2.Low.End(),
End: slice2.Rbrack + 1,
},
},
}...),
}},
})
}
for curFile := range filesUsing(inspect, info, "go1.21") {
file := curFile.Node().(*ast.File)
for curCall := range curFile.Preorder((*ast.CallExpr)(nil)) {
call := curCall.Node().(*ast.CallExpr)
if id, ok := call.Fun.(*ast.Ident); ok && len(call.Args) == 2 {
Expand All @@ -88,7 +91,7 @@ func slicesdelete(pass *analysis.Pass) {
equalSyntax(slice1.X, slice2.X) &&
increasingSliceIndices(info, slice1.High, slice2.Low) {
// Have append(s[:a], s[b:]...) where we can verify a < b.
report(call, slice1, slice2)
report(file, call, slice1, slice2)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package slicesdelete

import "slices"

import "slices"

import "slices"

import "slices"

import "slices"

import "slices"

import "slices"

import "slices"

var g struct{ f []int }

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

0 comments on commit 82317ce

Please sign in to comment.