Skip to content

Commit 122c93a

Browse files
adonovangopherbot
authored andcommitted
internal/refactor: AddImport: remove unnecessary result
And improve doc comment, and simplify the test. Change-Id: I8ea0e3e71152245d2b3e675585175a84dc18b750 Reviewed-on: https://go-review.googlesource.com/c/tools/+/710315 Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 76aace8 commit 122c93a

File tree

12 files changed

+74
-85
lines changed

12 files changed

+74
-85
lines changed

go/analysis/passes/inline/gofix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId inspector.Cursor) {
288288
} else if _, ok := importPrefixes[pkgPath]; !ok {
289289
// Use AddImport to add pkgPath if it's not there already. Associate the prefix it assigns
290290
// with the package path for use by the TypeString qualifier below.
291-
_, prefix, eds := refactor.AddImport(
291+
prefix, eds := refactor.AddImport(
292292
a.pass.TypesInfo, curFile, pkgName, pkgPath, tn.Name(), id.Pos())
293293
importPrefixes[pkgPath] = strings.TrimSuffix(prefix, ".")
294294
edits = append(edits, eds...)
@@ -459,7 +459,7 @@ func (a *analyzer) inlineConst(con *types.Const, cur inspector.Cursor) {
459459
edits []analysis.TextEdit
460460
)
461461
if incon.RHSPkgPath != a.pass.Pkg.Path() {
462-
_, importPrefix, edits = refactor.AddImport(
462+
importPrefix, edits = refactor.AddImport(
463463
a.pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos())
464464
}
465465
// If n is qualified by a package identifier, we'll need the full selector expression.

go/analysis/passes/modernize/maps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func mapsloop(pass *analysis.Pass) (any, error) {
166166

167167
// Report diagnostic, and suggest fix.
168168
rng := curRange.Node()
169-
_, prefix, importEdits := refactor.AddImport(info, file, "maps", "maps", funcName, rng.Pos())
169+
prefix, importEdits := refactor.AddImport(info, file, "maps", "maps", funcName, rng.Pos())
170170
var (
171171
newText []byte
172172
start, end token.Pos

go/analysis/passes/modernize/slices.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func appendclipped(pass *analysis.Pass) (any, error) {
164164
//
165165
// This is unsound if s is empty and its nilness
166166
// differs from zerocap (#73557).
167-
_, prefix, importEdits := refactor.AddImport(info, file, clonepkg, clonepkg, "Clone", call.Pos())
167+
prefix, importEdits := refactor.AddImport(info, file, clonepkg, clonepkg, "Clone", call.Pos())
168168
message := fmt.Sprintf("Replace append with %s.Clone", clonepkg)
169169
pass.Report(analysis.Diagnostic{
170170
Pos: call.Pos(),
@@ -185,7 +185,7 @@ func appendclipped(pass *analysis.Pass) (any, error) {
185185
// append(append(append(base, a...), b..., c...) -> slices.Concat(base, a, b, c)
186186
//
187187
// This is unsound if all slices are empty and base is non-nil (#73557).
188-
_, prefix, importEdits := refactor.AddImport(info, file, "slices", "slices", "Concat", call.Pos())
188+
prefix, importEdits := refactor.AddImport(info, file, "slices", "slices", "Concat", call.Pos())
189189
pass.Report(analysis.Diagnostic{
190190
Pos: call.Pos(),
191191
End: call.End(),

go/analysis/passes/modernize/slicescontains.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func slicescontains(pass *analysis.Pass) (any, error) {
202202
}
203203

204204
// Prepare slices.Contains{,Func} call.
205-
_, prefix, importEdits := refactor.AddImport(info, file, "slices", "slices", funcName, rng.Pos())
205+
prefix, importEdits := refactor.AddImport(info, file, "slices", "slices", funcName, rng.Pos())
206206
contains := fmt.Sprintf("%s%s(%s, %s)",
207207
prefix,
208208
funcName,

go/analysis/passes/modernize/slicesdelete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func slicesdelete(pass *analysis.Pass) (any, error) {
6262
return false
6363
}
6464

65-
_, prefix, edits := refactor.AddImport(info, file, "slices", "slices", "Delete", call.Pos())
65+
prefix, edits := refactor.AddImport(info, file, "slices", "slices", "Delete", call.Pos())
6666
// append's indices may be any integer type; slices.Delete requires int.
6767
// Insert int conversions as needed (and if possible).
6868
if isIntShadowed() && (!isIntExpr(slice1.High) || !isIntExpr(slice2.Low)) {

go/analysis/passes/modernize/sortslice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func slicessort(pass *analysis.Pass) (any, error) {
9090
fileUses(info, file, "go1.21") {
9191
// Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
9292

93-
_, prefix, importEdits := refactor.AddImport(
93+
prefix, importEdits := refactor.AddImport(
9494
info, file, "slices", "slices", "Sort", call.Pos())
9595

9696
pass.Report(analysis.Diagnostic{

go/analysis/passes/modernize/stringsbuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ nextcand:
102102
}
103103

104104
// Add strings import.
105-
_, prefix, importEdits := refactor.AddImport(
105+
prefix, importEdits := refactor.AddImport(
106106
pass.TypesInfo, astutil.EnclosingFile(def), "strings", "strings", "Builder", v.Pos())
107107
edits = append(edits, importEdits...)
108108

@@ -141,7 +141,7 @@ nextcand:
141141
// => var s strings.Builder; s.WriteString(expr)
142142

143143
// Add strings import.
144-
_, prefix, importEdits := refactor.AddImport(
144+
prefix, importEdits := refactor.AddImport(
145145
pass.TypesInfo, astutil.EnclosingFile(def), "strings", "strings", "Builder", v.Pos())
146146
edits = append(edits, importEdits...)
147147

go/analysis/passes/modernize/stringscutprefix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func stringscutprefix(pass *analysis.Pass) (any, error) {
128128
// shadow variables won't be valid because we only access the first statement (ditto Suffix).
129129
if astutil.EqualSyntax(s0, s) && astutil.EqualSyntax(pre0, pre) {
130130
after := refactor.FreshName(info.Scopes[ifStmt], ifStmt.Pos(), varName)
131-
_, prefix, importEdits := refactor.AddImport(
131+
prefix, importEdits := refactor.AddImport(
132132
info,
133133
curFile.Node().(*ast.File),
134134
obj1.Pkg().Name(),
@@ -213,7 +213,7 @@ func stringscutprefix(pass *analysis.Pass) (any, error) {
213213

214214
// We use AddImport not to add an import (since it exists already)
215215
// but to compute the correct prefix in the dot-import case.
216-
_, prefix, importEdits := refactor.AddImport(
216+
prefix, importEdits := refactor.AddImport(
217217
info,
218218
curFile.Node().(*ast.File),
219219
obj.Pkg().Name(),

go/analysis/passes/stringintconv/string.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func run(pass *analysis.Pass) (any, error) {
198198
// the type has methods, as some {String,GoString,Format}
199199
// may change the behavior of fmt.Sprint.
200200
if len(ttypes) == 1 && len(vtypes) == 1 && types.NewMethodSet(V0).Len() == 0 {
201-
_, prefix, importEdits := refactor.AddImport(pass.TypesInfo, file, "fmt", "fmt", "Sprint", arg.Pos())
201+
prefix, importEdits := refactor.AddImport(pass.TypesInfo, file, "fmt", "fmt", "Sprint", arg.Pos())
202202
if types.Identical(T0, types.Typ[types.String]) {
203203
// string(x) -> fmt.Sprint(x)
204204
addFix("Format the number as a decimal", append(importEdits,

gopls/internal/analysis/embeddirective/embeddirective.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func run(pass *analysis.Pass) (any, error) {
4747

4848
if !hasEmbedImport {
4949
// Add blank import of "embed".
50-
_, _, edits := refactor.AddImport(pass.TypesInfo, f, "_", "embed", "", c.Pos())
50+
_, edits := refactor.AddImport(pass.TypesInfo, f, "_", "embed", "", c.Pos())
5151
if len(edits) > 0 {
5252
pass.Report(analysis.Diagnostic{
5353
Pos: pos,

0 commit comments

Comments
 (0)