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>
diff --git a/go/analysis/passes/inline/gofix.go b/go/analysis/passes/inline/gofix.go
index fe1032c..1b3221f 100644
--- a/go/analysis/passes/inline/gofix.go
+++ b/go/analysis/passes/inline/gofix.go
@@ -172,7 +172,7 @@
a.pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err)
return
}
- curFile := currentFile(cur)
+ curFile := astutil.EnclosingFile(cur)
caller := &inline.Caller{
Fset: a.pass.Fset,
Types: a.pass.Pkg,
@@ -251,7 +251,7 @@
}
rhs := alias.Rhs()
curPath := a.pass.Pkg.Path()
- curFile := currentFile(curId)
+ curFile := astutil.EnclosingFile(curId)
id := curId.Node().(*ast.Ident)
// We have an identifier A here (n), possibly qualified by a package
// identifier (sel.n), and an inlinable "type A = rhs" elsewhere.
@@ -425,7 +425,7 @@
}
// If n is qualified by a package identifier, we'll need the full selector expression.
- curFile := currentFile(cur)
+ curFile := astutil.EnclosingFile(cur)
n := cur.Node().(*ast.Ident)
// We have an identifier A here (n), possibly qualified by a package identifier (sel.X,
@@ -503,14 +503,6 @@
return content, nil
}
-// currentFile returns the unique ast.File for a cursor.
-func currentFile(c inspector.Cursor) *ast.File {
- for cf := range c.Enclosing((*ast.File)(nil)) {
- return cf.Node().(*ast.File)
- }
- panic("no *ast.File enclosing a cursor: impossible")
-}
-
// A goFixInlineFuncFact is exported for each function marked "//go:fix inline".
// It holds information about the callee to support inlining.
type goFixInlineFuncFact struct{ Callee *inline.Callee }
diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go
index e68d0b0..bcbbfda 100644
--- a/gopls/internal/analysis/unusedparams/unusedparams.go
+++ b/gopls/internal/analysis/unusedparams/unusedparams.go
@@ -16,6 +16,7 @@
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/gopls/internal/util/moreslices"
"golang.org/x/tools/internal/analysisinternal"
+ "golang.org/x/tools/internal/astutil"
"golang.org/x/tools/internal/typesinternal"
)
@@ -243,10 +244,8 @@
// Don't report diagnostics on generated files.
// (We can't skip analysis of generated files, though.)
- for curFile := range c.Enclosing((*ast.File)(nil)) {
- if ast.IsGenerated(curFile.Node().(*ast.File)) {
- continue funcloop
- }
+ if ast.IsGenerated(astutil.EnclosingFile(c)) {
+ continue funcloop
}
// Report each unused parameter.