Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: implement Type.OfKind filter #334

Merged
merged 1 commit into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions analyzer/testdata/src/filtertest/f1.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,103 @@ func detectType() {
typeTest(1, "no", 3, "variadic underlying int") // want `false`
typeTest(1, 2, "no", "variadic underlying int") // want `false`
}

{
type myInt int
typeTest(1, "is numeric") // want `true`
typeTest(myInt(1), "is numeric")
typeTest("not numeric", "is numeric")

typeTest(1, "underlying is numeric") // want `true`
typeTest(1.63, "underlying is numeric") // want `true`
typeTest(myInt(1), "underlying is numeric") // want `true`
typeTest("not", "underlying is numeric")
typeTest([]int{1}, "underlying is numeric")

typeTest(uintptr(5), "is unsigned") // want `true`
typeTest(uint(5), "is unsigned") // want `true`
typeTest(uint8(5), "is unsigned") // want `true`
typeTest(uint16(5), "is unsigned") // want `true`
typeTest(uint32(5), "is unsigned") // want `true`
typeTest(uint64(5), "is unsigned") // want `true`
typeTest(5, "is unsigned")
typeTest(int32(5), "is unsigned")
typeTest(int(5), "is unsigned")
typeTest(rune(5), "is unsigned")
typeTest("934", "is unsigned")
typeTest(4.6, "is unsigned")

typeTest(uint(5), "is signed")
typeTest(uint8(5), "is signed")
typeTest(uint16(5), "is signed")
typeTest(uint32(5), "is signed")
typeTest(uint64(5), "is signed")
typeTest(uintptr(5), "is signed")
typeTest(5, "is signed") // want `true`
typeTest(int8(5), "is signed") // want `true`
typeTest(int16(5), "is signed") // want `true`
typeTest(int32(5), "is signed") // want `true`
typeTest(int64(5), "is signed") // want `true`
typeTest(int(5), "is signed") // want `true`
typeTest(rune(5), "is signed") // want `true`
typeTest("934", "is signed")
typeTest(4.6, "is signed")
typeTest([]int{225}, "is signed")

typeTest(uint(5), "is float")
typeTest(uint8(5), "is float")
typeTest(uint16(5), "is float")
typeTest(uint32(5), "is float")
typeTest(uint64(5), "is float")
typeTest(uintptr(5), "is float")
typeTest(5, "is float")
typeTest(int8(5), "is float")
typeTest(int16(5), "is float")
typeTest(int32(5), "is float")
typeTest(int64(5), "is float")
typeTest(int(5), "is float")
typeTest("934", "is float")
typeTest(4.6, "is float") // want `true`
typeTest(float32(4.6), "is float") // want `true`
typeTest(float64(4.6), "is float") // want `true`
typeTest([]int{225}, "is float")

typeTest(5, "is int") // want `true`
typeTest(int8(1), "is int") // want `true`
typeTest(int16(1), "is int") // want `true`
typeTest(int32(1), "is int") // want `true`
typeTest(int64(1), "is int") // want `true`
typeTest(rune(1), "is int") // want `true`
typeTest(byte(1), "is int")
typeTest(uint(1), "is int")
typeTest(uint8(1), "is int")
typeTest(uint16(1), "is int")
typeTest(uint32(1), "is int")
typeTest(uint64(1), "is int")
typeTest([]int{3}, "is int")
typeTest("ds", "is int")
typeTest(54.2, "is int")
typeTest(float64(5.3), "is int")
typeTest(float32(5.3), "is int")

typeTest(5, "is uint")
typeTest(int8(1), "is uint")
typeTest(int16(1), "is uint")
typeTest(int32(1), "is uint")
typeTest(int64(1), "is uint")
typeTest(rune(1), "is uint")
typeTest(byte(1), "is uint") // want `true`
typeTest(uint(1), "is uint") // want `true`
typeTest(uint8(1), "is uint") // want `true`
typeTest(uint16(1), "is uint") // want `true`
typeTest(uint32(1), "is uint") // want `true`
typeTest(uint64(1), "is uint") // want `true`
typeTest([]int{3}, "is uint")
typeTest("ds", "is uint")
typeTest(54.2, "is uint")
typeTest(float64(5.3), "is uint")
typeTest(float32(5.3), "is uint")
}
}

func detectAddressable(x int, xs []int) {
Expand Down
28 changes: 28 additions & 0 deletions analyzer/testdata/src/filtertest/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,32 @@ func testRules(m dsl.Matcher) {
m.Match(`textTest("", "root text test")`).
Where(m["$$"].Text == `textTest("", "root text test")`).
Report(`true`)

m.Match(`typeTest($x, "is numeric")`).
Where(m["x"].Type.OfKind("numeric")).
Report(`true`)

m.Match(`typeTest($x, "underlying is numeric")`).
Where(m["x"].Type.Underlying().OfKind("numeric")).
Report(`true`)

m.Match(`typeTest($x, "is unsigned")`).
Where(m["x"].Type.Underlying().OfKind("unsigned")).
Report(`true`)

m.Match(`typeTest($x, "is signed")`).
Where(m["x"].Type.Underlying().OfKind("signed")).
Report(`true`)

m.Match(`typeTest($x, "is float")`).
Where(m["x"].Type.Underlying().OfKind("float")).
Report(`true`)

m.Match(`typeTest($x, "is int")`).
Where(m["x"].Type.Underlying().OfKind("int")).
Report(`true`)

m.Match(`typeTest($x, "is uint")`).
Where(m["x"].Type.Underlying().OfKind("uint")).
Report(`true`)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/go-toolsmith/astcopy v1.0.0
github.com/google/go-cmp v0.5.6
github.com/quasilyte/go-ruleguard/dsl v0.3.10
github.com/quasilyte/go-ruleguard/dsl v0.3.11
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71
github.com/quasilyte/gogrep v0.0.0-20211226113550-e12a97c7d96d
golang.org/x/tools v0.0.0-20201230224404-63754364767c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/quasilyte/go-ruleguard v0.3.1-0.20210203134552-1b5a410e1cc8/go.mod h1
github.com/quasilyte/go-ruleguard/dsl v0.3.0/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/dsl v0.3.10 h1:4tVlVVcBT+nNWoF+t/zrAMO13sHAqYotX1K12Gc8f8A=
github.com/quasilyte/go-ruleguard/dsl v0.3.10/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/dsl v0.3.11 h1:TOyCsB86VE915JMP8ErXf12k+OvIYNkBN6D/waKQih4=
github.com/quasilyte/go-ruleguard/dsl v0.3.11/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20201231183845-9e62ed36efe1/go.mod h1:7JTjp89EGyU1d6XfBiXihJNG37wB2VRkd125Q1u7Plc=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71 h1:CNooiryw5aisadVfzneSZPswRWvnVW8hF1bS/vo8ReI=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50=
Expand Down
47 changes: 47 additions & 0 deletions ruleguard/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,53 @@ func makeTypeImplementsFilter(src, varname string, iface *types.Interface) filte
}
}

func makeTypeIsIntUintFilter(src, varname string, underlying bool, kind types.BasicKind) filterFunc {
return func(params *filterParams) matchFilterResult {
typ := params.typeofNode(params.subExpr(varname))
if underlying {
typ = typ.Underlying()
}
if basicType, ok := typ.(*types.Basic); ok {
first := kind
last := kind + 4
if basicType.Kind() >= first && basicType.Kind() <= last {
return filterSuccess
}
}
return filterFailure(src)
}
}

func makeTypeIsSignedFilter(src, varname string, underlying bool) filterFunc {
return func(params *filterParams) matchFilterResult {
typ := params.typeofNode(params.subExpr(varname))
if underlying {
typ = typ.Underlying()
}
if basicType, ok := typ.(*types.Basic); ok {
if basicType.Info()&types.IsInteger != 0 && basicType.Info()&types.IsUnsigned == 0 {
return filterSuccess
}
}
return filterFailure(src)
}
}

func makeTypeOfKindFilter(src, varname string, underlying bool, kind types.BasicInfo) filterFunc {
return func(params *filterParams) matchFilterResult {
typ := params.typeofNode(params.subExpr(varname))
if underlying {
typ = typ.Underlying()
}
if basicType, ok := typ.(*types.Basic); ok {
if basicType.Info()&kind != 0 {
return filterSuccess
}
}
return filterFailure(src)
}
}

func makeTypeIsFilter(src, varname string, underlying bool, pat *typematch.Pattern) filterFunc {
if underlying {
return func(params *filterParams) matchFilterResult {
Expand Down
Loading