Skip to content

Commit 4bbcc9f

Browse files
cuishuanggopherbot
authored andcommitted
all: use reflect.TypeFor instead of reflect.TypeOf when we have known the type
For golang/go#60088. Change-Id: I43a47ae20cd84e02b0eb3496387f73d92745da58 Reviewed-on: https://go-review.googlesource.com/c/tools/+/709135 Reviewed-by: Sean Liao <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Sean Liao <[email protected]>
1 parent 122c93a commit 4bbcc9f

File tree

8 files changed

+20
-20
lines changed

8 files changed

+20
-20
lines changed

go/analysis/passes/pkgfact/pkgfact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var Analyzer = &analysis.Analyzer{
4141
URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/pkgfact",
4242
Run: run,
4343
FactTypes: []analysis.Fact{new(pairsFact)},
44-
ResultType: reflect.TypeOf(map[string]string{}),
44+
ResultType: reflect.TypeFor[map[string]string](),
4545
}
4646

4747
// A pairsFact is a package-level fact that records

go/analysis/passes/printf/printf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var Analyzer = &analysis.Analyzer{
4343
URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/printf",
4444
Requires: []*analysis.Analyzer{inspect.Analyzer},
4545
Run: run,
46-
ResultType: reflect.TypeOf((*Result)(nil)),
46+
ResultType: reflect.TypeFor[*Result](),
4747
FactTypes: []analysis.Fact{new(isWrapper)},
4848
}
4949

go/analysis/passes/usesgenerics/usesgenerics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var Analyzer = &analysis.Analyzer{
2424
URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/usesgenerics",
2525
Requires: []*analysis.Analyzer{inspect.Analyzer},
2626
Run: run,
27-
ResultType: reflect.TypeOf((*Result)(nil)),
27+
ResultType: reflect.TypeFor[*Result](),
2828
FactTypes: []analysis.Fact{new(featuresFact)},
2929
}
3030

internal/analysisinternal/generated/generated.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var Analyzer = &analysis.Analyzer{
1818
Name: "generated",
1919
Doc: "detect which Go files are generated",
2020
URL: "https://pkg.go.dev/golang.org/x/tools/internal/analysisinternal/generated",
21-
ResultType: reflect.TypeOf((*Result)(nil)),
21+
ResultType: reflect.TypeFor[*Result](),
2222
Run: func(pass *analysis.Pass) (any, error) {
2323
set := make(map[*token.File]bool)
2424
for _, file := range pass.Files {

internal/facts/facts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func TestFactFilter(t *testing.T) {
461461
s.ExportPackageFact(&otherFact{"bad package fact"})
462462

463463
filter := map[reflect.Type]bool{
464-
reflect.TypeOf(&myFact{}): true,
464+
reflect.TypeFor[*myFact](): true,
465465
}
466466

467467
pkgFacts := s.AllPackageFacts(filter)

internal/packagestest/expect.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ func (e *Exported) getMarkers() error {
228228
}
229229

230230
var (
231-
noteType = reflect.TypeOf((*expect.Note)(nil))
232-
identifierType = reflect.TypeOf(expect.Identifier(""))
233-
posType = reflect.TypeOf(token.Pos(0))
234-
positionType = reflect.TypeOf(token.Position{})
235-
rangeType = reflect.TypeOf(Range{})
236-
fsetType = reflect.TypeOf((*token.FileSet)(nil))
237-
regexType = reflect.TypeOf((*regexp.Regexp)(nil))
238-
exportedType = reflect.TypeOf((*Exported)(nil))
231+
noteType = reflect.TypeFor[*expect.Note]()
232+
identifierType = reflect.TypeFor[expect.Identifier]()
233+
posType = reflect.TypeFor[token.Pos]()
234+
positionType = reflect.TypeFor[token.Position]()
235+
rangeType = reflect.TypeFor[Range]()
236+
fsetType = reflect.TypeFor[*token.FileSet]()
237+
regexType = reflect.TypeFor[*regexp.Regexp]()
238+
exportedType = reflect.TypeFor[*Exported]()
239239
)
240240

241241
// converter converts from a marker's argument parsed from the comment to

internal/refactor/inline/inline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,7 @@ func cleanNodes[T ast.Node](nodes []T) []T {
31393139
//
31403140
// TODO(adonovan): remove this horrendous workaround when #20744 is finally fixed.
31413141
func clearPositions(root ast.Node) {
3142-
posType := reflect.TypeOf(token.NoPos)
3142+
posType := reflect.TypeFor[token.Pos]()
31433143
ast.Inspect(root, func(n ast.Node) bool {
31443144
if n != nil {
31453145
v := reflect.ValueOf(n).Elem() // deref the pointer to struct

refactor/eg/rewrite.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ var (
158158
objectPtrNil = reflect.ValueOf((*ast.Object)(nil))
159159
scopePtrNil = reflect.ValueOf((*ast.Scope)(nil))
160160

161-
identType = reflect.TypeOf((*ast.Ident)(nil))
162-
selectorExprType = reflect.TypeOf((*ast.SelectorExpr)(nil))
163-
objectPtrType = reflect.TypeOf((*ast.Object)(nil))
164-
statementType = reflect.TypeOf((*ast.Stmt)(nil)).Elem()
165-
positionType = reflect.TypeOf(token.NoPos)
166-
scopePtrType = reflect.TypeOf((*ast.Scope)(nil))
161+
identType = reflect.TypeFor[*ast.Ident]()
162+
selectorExprType = reflect.TypeFor[*ast.SelectorExpr]()
163+
objectPtrType = reflect.TypeFor[*ast.Object]()
164+
statementType = reflect.TypeFor[ast.Stmt]()
165+
positionType = reflect.TypeFor[token.Pos]()
166+
scopePtrType = reflect.TypeFor[*ast.Scope]()
167167
)
168168

169169
// apply replaces each AST field x in val with f(x), returning val.

0 commit comments

Comments
 (0)