Skip to content

Commit

Permalink
ruleguard/typematch: handle unsafe.Pointer correctly (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte authored Jan 16, 2022
1 parent 1e92ea5 commit b223b6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ruleguard/typematch/typematch.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ func parseExpr(ctx *Context, e ast.Expr) *pattern {
if !ok {
return nil
}
if pkg.Name == "unsafe" && e.Sel.Name == "Pointer" {
return &pattern{op: opBuiltinType, value: types.Typ[types.UnsafePointer]}
}
pkgPath, ok := ctx.Itab.Lookup(pkg.Name)
if !ok {
return nil
Expand Down
18 changes: 13 additions & 5 deletions ruleguard/typematch/typematch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

var (
typeInt = types.Typ[types.Int]
typeString = types.Typ[types.String]
typeInt32 = types.Typ[types.Int32]
typeUint8 = types.Typ[types.Uint8]
typeEstruct = types.NewStruct(nil, nil)
typeInt = types.Typ[types.Int]
typeString = types.Typ[types.String]
typeInt32 = types.Typ[types.Int32]
typeUint8 = types.Typ[types.Uint8]
typeUnsafePtr = types.Typ[types.UnsafePointer]
typeEstruct = types.NewStruct(nil, nil)

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

{`unsafe.Pointer`, typeUnsafePtr},
{`[]unsafe.Pointer`, types.NewSlice(typeUnsafePtr)},

{`func()`, types.NewSignature(nil, nil, nil, false)},
{`func(int)`, types.NewSignature(nil, types.NewTuple(intVar), nil, false)},
{`func(int, string)`, types.NewSignature(nil, types.NewTuple(intVar, stringVar), nil, false)},
Expand Down Expand Up @@ -176,6 +180,10 @@ func TestIdenticalNegative(t *testing.T) {
{`map[int]int`, types.NewMap(typeString, typeInt)},
{`map[int]int`, types.NewMap(typeInt, typeString)},

{`unsafe.Pointer`, typeInt},
{`unsafe.Pointer`, types.NewPointer(typeInt)},
{`[]unsafe.Pointer`, types.NewSlice(typeInt)},

{`interface{}`, typeInt},
{`interface{ $*_ }`, typeString},
{`interface{ $*_ }`, types.NewArray(typeString, 10)},
Expand Down

0 comments on commit b223b6f

Please sign in to comment.