Skip to content

Commit d507345

Browse files
adonovangopherbot
authored andcommitted
x/tools: use new astutil.EnclosingFile helper
Change-Id: Iec730e8decfee3d1cba818155c59ddb4ef001476 Reviewed-on: https://go-review.googlesource.com/c/tools/+/710715 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com>
1 parent a22b5e8 commit d507345

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

go/analysis/passes/inline/gofix.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (a *analyzer) inlineCall(call *ast.CallExpr, cur inspector.Cursor) {
172172
a.pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err)
173173
return
174174
}
175-
curFile := currentFile(cur)
175+
curFile := astutil.EnclosingFile(cur)
176176
caller := &inline.Caller{
177177
Fset: a.pass.Fset,
178178
Types: a.pass.Pkg,
@@ -251,7 +251,7 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId inspector.Cursor) {
251251
}
252252
rhs := alias.Rhs()
253253
curPath := a.pass.Pkg.Path()
254-
curFile := currentFile(curId)
254+
curFile := astutil.EnclosingFile(curId)
255255
id := curId.Node().(*ast.Ident)
256256
// We have an identifier A here (n), possibly qualified by a package
257257
// identifier (sel.n), and an inlinable "type A = rhs" elsewhere.
@@ -425,7 +425,7 @@ func (a *analyzer) inlineConst(con *types.Const, cur inspector.Cursor) {
425425
}
426426

427427
// If n is qualified by a package identifier, we'll need the full selector expression.
428-
curFile := currentFile(cur)
428+
curFile := astutil.EnclosingFile(cur)
429429
n := cur.Node().(*ast.Ident)
430430

431431
// We have an identifier A here (n), possibly qualified by a package identifier (sel.X,
@@ -503,14 +503,6 @@ func (a *analyzer) readFile(node ast.Node) ([]byte, error) {
503503
return content, nil
504504
}
505505

506-
// currentFile returns the unique ast.File for a cursor.
507-
func currentFile(c inspector.Cursor) *ast.File {
508-
for cf := range c.Enclosing((*ast.File)(nil)) {
509-
return cf.Node().(*ast.File)
510-
}
511-
panic("no *ast.File enclosing a cursor: impossible")
512-
}
513-
514506
// A goFixInlineFuncFact is exported for each function marked "//go:fix inline".
515507
// It holds information about the callee to support inlining.
516508
type goFixInlineFuncFact struct{ Callee *inline.Callee }

gopls/internal/analysis/unusedparams/unusedparams.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/tools/go/ast/inspector"
1717
"golang.org/x/tools/gopls/internal/util/moreslices"
1818
"golang.org/x/tools/internal/analysisinternal"
19+
"golang.org/x/tools/internal/astutil"
1920
"golang.org/x/tools/internal/typesinternal"
2021
)
2122

@@ -243,10 +244,8 @@ funcloop:
243244

244245
// Don't report diagnostics on generated files.
245246
// (We can't skip analysis of generated files, though.)
246-
for curFile := range c.Enclosing((*ast.File)(nil)) {
247-
if ast.IsGenerated(curFile.Node().(*ast.File)) {
248-
continue funcloop
249-
}
247+
if ast.IsGenerated(astutil.EnclosingFile(c)) {
248+
continue funcloop
250249
}
251250

252251
// Report each unused parameter.

0 commit comments

Comments
 (0)