Skip to content

Commit b223b6f

Browse files
authored
ruleguard/typematch: handle unsafe.Pointer correctly (#359)
1 parent 1e92ea5 commit b223b6f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

ruleguard/typematch/typematch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ func parseExpr(ctx *Context, e ast.Expr) *pattern {
168168
if !ok {
169169
return nil
170170
}
171+
if pkg.Name == "unsafe" && e.Sel.Name == "Pointer" {
172+
return &pattern{op: opBuiltinType, value: types.Typ[types.UnsafePointer]}
173+
}
171174
pkgPath, ok := ctx.Itab.Lookup(pkg.Name)
172175
if !ok {
173176
return nil

ruleguard/typematch/typematch_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
)
99

1010
var (
11-
typeInt = types.Typ[types.Int]
12-
typeString = types.Typ[types.String]
13-
typeInt32 = types.Typ[types.Int32]
14-
typeUint8 = types.Typ[types.Uint8]
15-
typeEstruct = types.NewStruct(nil, nil)
11+
typeInt = types.Typ[types.Int]
12+
typeString = types.Typ[types.String]
13+
typeInt32 = types.Typ[types.Int32]
14+
typeUint8 = types.Typ[types.Uint8]
15+
typeUnsafePtr = types.Typ[types.UnsafePointer]
16+
typeEstruct = types.NewStruct(nil, nil)
1617

1718
stringerIface = types.NewInterfaceType([]*types.Func{
1819
types.NewFunc(token.NoPos, nil, "String",
@@ -91,6 +92,9 @@ func TestIdentical(t *testing.T) {
9192
{`[]rune`, types.NewSlice(typeInt32)},
9293
{`[8]byte`, types.NewArray(typeUint8, 8)},
9394

95+
{`unsafe.Pointer`, typeUnsafePtr},
96+
{`[]unsafe.Pointer`, types.NewSlice(typeUnsafePtr)},
97+
9498
{`func()`, types.NewSignature(nil, nil, nil, false)},
9599
{`func(int)`, types.NewSignature(nil, types.NewTuple(intVar), nil, false)},
96100
{`func(int, string)`, types.NewSignature(nil, types.NewTuple(intVar, stringVar), nil, false)},
@@ -176,6 +180,10 @@ func TestIdenticalNegative(t *testing.T) {
176180
{`map[int]int`, types.NewMap(typeString, typeInt)},
177181
{`map[int]int`, types.NewMap(typeInt, typeString)},
178182

183+
{`unsafe.Pointer`, typeInt},
184+
{`unsafe.Pointer`, types.NewPointer(typeInt)},
185+
{`[]unsafe.Pointer`, types.NewSlice(typeInt)},
186+
179187
{`interface{}`, typeInt},
180188
{`interface{ $*_ }`, typeString},
181189
{`interface{ $*_ }`, types.NewArray(typeString, 10)},

0 commit comments

Comments
 (0)