From bf5d305cd56dfc217569f26388e55d2dbb41e9da Mon Sep 17 00:00:00 2001 From: Iskander Sharipov Date: Sun, 30 Jan 2022 21:21:03 +0300 Subject: [PATCH] ruleguard: use stdinfo PathByName and don't make excessive copies --- ruleguard/engine.go | 4 ++-- ruleguard/typematch/typematch.go | 9 ++------- ruleguard/typematch/typematch_test.go | 11 ++++------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/ruleguard/engine.go b/ruleguard/engine.go index 444e8a3..52b85fd 100644 --- a/ruleguard/engine.go +++ b/ruleguard/engine.go @@ -64,7 +64,7 @@ func (e *engine) Load(ctx *LoadContext, buildContext *build.Context, filename st pkg: pkg, ctx: ctx, importer: imp, - itab: typematch.NewImportsTab(stdinfo.PackagesList), + itab: typematch.NewImportsTab(stdinfo.PathByName), gogrepFset: token.NewFileSet(), } l := newIRLoader(config) @@ -97,7 +97,7 @@ func (e *engine) LoadFromIR(ctx *LoadContext, buildContext *build.Context, filen state: e.state, ctx: ctx, importer: imp, - itab: typematch.NewImportsTab(stdinfo.PackagesList), + itab: typematch.NewImportsTab(stdinfo.PathByName), gogrepFset: token.NewFileSet(), } l := newIRLoader(config) diff --git a/ruleguard/typematch/typematch.go b/ruleguard/typematch/typematch.go index 812b6d9..4363e6f 100644 --- a/ruleguard/typematch/typematch.go +++ b/ruleguard/typematch/typematch.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/quasilyte/go-ruleguard/internal/xtypes" - "github.com/quasilyte/stdinfo" ) //go:generate stringer -type=patternOp @@ -61,12 +60,8 @@ type ImportsTab struct { imports []map[string]string } -func NewImportsTab(initial []stdinfo.Package) *ImportsTab { - convertMap := make(map[string]string) - for _, pack := range initial { - convertMap[pack.Name] = pack.Path - } - return &ImportsTab{imports: []map[string]string{convertMap}} +func NewImportsTab(initial map[string]string) *ImportsTab { + return &ImportsTab{imports: []map[string]string{initial}} } func (itab *ImportsTab) Lookup(pkgName string) (string, bool) { diff --git a/ruleguard/typematch/typematch_test.go b/ruleguard/typematch/typematch_test.go index 55aa618..c616266 100644 --- a/ruleguard/typematch/typematch_test.go +++ b/ruleguard/typematch/typematch_test.go @@ -5,8 +5,6 @@ import ( "go/types" "path" "testing" - - "github.com/quasilyte/stdinfo" ) var ( @@ -16,10 +14,6 @@ var ( typeUint8 = types.Typ[types.Uint8] typeUnsafePtr = types.Typ[types.UnsafePointer] typeEstruct = types.NewStruct(nil, nil) - packages = [2]stdinfo.Package{ - {Name: "io", Path: "io"}, - {Name: "syntax", Path: "regexp/syntax"}, - } stringerIface = types.NewInterfaceType([]*types.Func{ types.NewFunc(token.NoPos, nil, "String", @@ -32,7 +26,10 @@ var ( stringVar = types.NewVar(token.NoPos, nil, "_", typeString) testContext = &Context{ - Itab: NewImportsTab(packages[:]), + Itab: NewImportsTab(map[string]string{ + "io": "io", + "syntax": "regexp/syntax", + }), } )