Skip to content

Commit

Permalink
checkers: don't call SizeOf for type parameters (#1237)
Browse files Browse the repository at this point in the history
StdSizes.SizeOf method doesn't support type parameters
  • Loading branch information
mcdoker18 committed Aug 9, 2022
1 parent 8df9d7e commit af0e6d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions checkers/hugeParam_checker.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/framework/linter"
"golang.org/x/exp/typeparams"
)

func init() {
Expand Down Expand Up @@ -49,6 +50,9 @@ func (c *hugeParamChecker) checkParams(params []*ast.Field) {
for _, p := range params {
for _, id := range p.Names {
typ := c.ctx.TypeOf(id)
if _, ok := typ.(*typeparams.TypeParam); ok {
continue
}
size := c.ctx.SizesInfo.Sizeof(typ)
if size >= c.sizeThreshold {
c.warn(id, size)
Expand Down
4 changes: 4 additions & 0 deletions checkers/rangeValCopy_checker.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/framework/linter"
"golang.org/x/exp/typeparams"
)

func init() {
Expand Down Expand Up @@ -65,6 +66,9 @@ func (c *rangeValCopyChecker) VisitStmt(stmt ast.Stmt) {
if typ == nil {
return
}
if _, ok := typ.(*typeparams.TypeParam); ok {
return
}
if size := c.ctx.SizesInfo.Sizeof(typ); size >= c.sizeThreshold {
c.warn(rng, size)
}
Expand Down
6 changes: 6 additions & 0 deletions checkers/testdata/hugeParam/negative_tests_go118.go
@@ -0,0 +1,6 @@
//go:build go1.18
// +build go1.18

package checker_test

func genericFunc[T comparable](x T) {}
11 changes: 11 additions & 0 deletions checkers/testdata/rangeValCopy/negative_tests_go118.go
@@ -0,0 +1,11 @@
//go:build go1.18
// +build go1.18

package checker_test

func genericSlice[T any](original []T, f func(T)) {
for _, v := range original {
// OK: v is a type parameter.
f(v)
}
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -16,6 +16,7 @@ require (
github.com/quasilyte/go-ruleguard v0.3.16-0.20220213074421-6aa060fab41a
github.com/quasilyte/go-ruleguard/dsl v0.3.21
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da
)

0 comments on commit af0e6d7

Please sign in to comment.