Skip to content

Commit a4ee291

Browse files
authored
ruleguard: use stdinfo PathByName and don't make excessive copies (#373)
1 parent 8479df4 commit a4ee291

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

ruleguard/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (e *engine) Load(ctx *LoadContext, buildContext *build.Context, filename st
6464
pkg: pkg,
6565
ctx: ctx,
6666
importer: imp,
67-
itab: typematch.NewImportsTab(stdinfo.PackagesList),
67+
itab: typematch.NewImportsTab(stdinfo.PathByName),
6868
gogrepFset: token.NewFileSet(),
6969
}
7070
l := newIRLoader(config)
@@ -97,7 +97,7 @@ func (e *engine) LoadFromIR(ctx *LoadContext, buildContext *build.Context, filen
9797
state: e.state,
9898
ctx: ctx,
9999
importer: imp,
100-
itab: typematch.NewImportsTab(stdinfo.PackagesList),
100+
itab: typematch.NewImportsTab(stdinfo.PathByName),
101101
gogrepFset: token.NewFileSet(),
102102
}
103103
l := newIRLoader(config)

ruleguard/typematch/typematch.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111

1212
"github.com/quasilyte/go-ruleguard/internal/xtypes"
13-
"github.com/quasilyte/stdinfo"
1413
)
1514

1615
//go:generate stringer -type=patternOp
@@ -61,12 +60,8 @@ type ImportsTab struct {
6160
imports []map[string]string
6261
}
6362

64-
func NewImportsTab(initial []stdinfo.Package) *ImportsTab {
65-
convertMap := make(map[string]string)
66-
for _, pack := range initial {
67-
convertMap[pack.Name] = pack.Path
68-
}
69-
return &ImportsTab{imports: []map[string]string{convertMap}}
63+
func NewImportsTab(initial map[string]string) *ImportsTab {
64+
return &ImportsTab{imports: []map[string]string{initial}}
7065
}
7166

7267
func (itab *ImportsTab) Lookup(pkgName string) (string, bool) {

ruleguard/typematch/typematch_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"go/types"
66
"path"
77
"testing"
8-
9-
"github.com/quasilyte/stdinfo"
108
)
119

1210
var (
@@ -16,10 +14,6 @@ var (
1614
typeUint8 = types.Typ[types.Uint8]
1715
typeUnsafePtr = types.Typ[types.UnsafePointer]
1816
typeEstruct = types.NewStruct(nil, nil)
19-
packages = [2]stdinfo.Package{
20-
{Name: "io", Path: "io"},
21-
{Name: "syntax", Path: "regexp/syntax"},
22-
}
2317

2418
stringerIface = types.NewInterfaceType([]*types.Func{
2519
types.NewFunc(token.NoPos, nil, "String",
@@ -32,7 +26,10 @@ var (
3226
stringVar = types.NewVar(token.NoPos, nil, "_", typeString)
3327

3428
testContext = &Context{
35-
Itab: NewImportsTab(packages[:]),
29+
Itab: NewImportsTab(map[string]string{
30+
"io": "io",
31+
"syntax": "regexp/syntax",
32+
}),
3633
}
3734
)
3835

0 commit comments

Comments
 (0)