Skip to content

Commit

Permalink
ruleguard/typematch: add interface{$*_} pattern support (#236)
Browse files Browse the repository at this point in the history
Makes it possible to match "any interface" type.
  • Loading branch information
quasilyte authored May 19, 2021
1 parent 2cb006b commit f8e9dc0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
19 changes: 19 additions & 0 deletions analyzer/testdata/src/filtertest/f1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filtertest

import (
"errors"
"fmt"
"os"
"time"
)
Expand Down Expand Up @@ -49,6 +50,24 @@ func assignableTo() {
}

func detectType() {
{
var s fmt.Stringer

typeTest(s, "is interface")
typeTest(interface{}(nil), "is interface") // want `YES`
typeTest(implementsAll{}, "is interface")
typeTest(&implementsAll{}, "is interface")
typeTest(4, "is interface")
typeTest("", "is interface")

typeTest(s, "underlying is interface") // want `YES`
typeTest(interface{}(nil), "underlying is interface") // want `YES`
typeTest(implementsAll{}, "underlying is interface")
typeTest(&implementsAll{}, "underlying is interface")
typeTest(4, "underlying is interface")
typeTest("", "underlying is interface")
}

{
type withNamedTime struct {
x int
Expand Down
8 changes: 8 additions & 0 deletions analyzer/testdata/src/filtertest/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,12 @@ func testRules(m dsl.Matcher) {
m.Match(`typeTest($x, "assignable to interface{}")`).
Where(m["x"].Type.AssignableTo(`interface{}`)).
Report(`YES`)

m.Match(`typeTest($x, "is interface")`).
Where(m["x"].Type.Is(`interface{ $*_ }`)).
Report(`YES`)

m.Match(`typeTest($x, "underlying is interface")`).
Where(m["x"].Type.Underlying().Is(`interface{ $*_ }`)).
Report(`YES`)
}
7 changes: 4 additions & 3 deletions ruleguard/typematch/patternop_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions ruleguard/typematch/typematch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
opFunc
opStructNoSeq
opStruct
opAnyInterface
opNamed
)

Expand Down Expand Up @@ -313,6 +314,16 @@ func parseExpr(ctx *Context, e ast.Expr) *pattern {
if len(e.Methods.List) == 0 {
return &pattern{op: opBuiltinType, value: efaceType}
}
if len(e.Methods.List) == 1 {
p := parseExpr(ctx, e.Methods.List[0].Type)
if p == nil {
return nil
}
if p.op != opVarSeq {
return nil
}
return &pattern{op: opAnyInterface}
}
}

return nil
Expand Down Expand Up @@ -525,6 +536,10 @@ func (p *Pattern) matchIdentical(sub *pattern, typ types.Type) bool {
}
return true

case opAnyInterface:
_, ok := typ.(*types.Interface)
return ok

default:
return false
}
Expand Down
10 changes: 10 additions & 0 deletions ruleguard/typematch/typematch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var (
typeUint8 = types.Typ[types.Uint8]
typeEstruct = types.NewStruct(nil, nil)

stringerIface = types.NewInterfaceType([]*types.Func{
types.NewFunc(token.NoPos, nil, "String",
types.NewSignature(nil, types.NewTuple(), types.NewTuple(types.NewVar(token.NoPos, nil, "result", typeString)), false)),
}, nil)

intVar = types.NewVar(token.NoPos, nil, "_", typeInt)
int32Var = types.NewVar(token.NoPos, nil, "_", typeInt32)
estructVar = types.NewVar(token.NoPos, nil, "_", typeEstruct)
Expand Down Expand Up @@ -55,7 +60,9 @@ func TestIdentical(t *testing.T) {
{`[][]int`, types.NewSlice(types.NewSlice(typeInt))},
{`[10]int`, types.NewArray(typeInt, 10)},
{`map[int]int`, types.NewMap(typeInt, typeInt)},

{`interface{}`, types.NewInterfaceType(nil, nil)},
{`interface{ $*_ }`, stringerIface},

{`$t`, typeInt},
{`*$t`, types.NewPointer(typeInt)},
Expand Down Expand Up @@ -159,7 +166,10 @@ func TestIdenticalNegative(t *testing.T) {
{`map[int]int`, types.NewMap(typeString, typeString)},
{`map[int]int`, types.NewMap(typeString, typeInt)},
{`map[int]int`, types.NewMap(typeInt, typeString)},

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

{`*$t`, typeInt},
{`map[$t]$t`, types.NewMap(typeString, typeInt)},
Expand Down

0 comments on commit f8e9dc0

Please sign in to comment.