Skip to content

Commit

Permalink
checkers: use ctx.SizeOf instead of SizesInfo.SizeOf (#1247)
Browse files Browse the repository at this point in the history
Also add a ruleguard rule to avoid the unwanted sizeof form
in the future.
  • Loading branch information
quasilyte committed Aug 9, 2022
1 parent f1528e1 commit dfd27d0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions checkers/hugeParam_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (c *hugeParamChecker) checkParams(params []*ast.Field) {
if _, ok := typ.(*typeparams.TypeParam); ok {
continue
}
size := c.ctx.SizesInfo.Sizeof(typ)
if size >= c.sizeThreshold {
size, ok := c.ctx.SizeOf(typ)
if ok && size >= c.sizeThreshold {
c.warn(id, size)
}
}
Expand Down
2 changes: 1 addition & 1 deletion checkers/rangeExprCopy_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *rangeExprCopyChecker) VisitStmt(stmt ast.Stmt) {
if _, ok := tv.Type.(*types.Array); !ok {
return
}
if size := c.ctx.SizesInfo.Sizeof(tv.Type); size >= c.sizeThreshold {
if size, ok := c.ctx.SizeOf(tv.Type); ok && size >= c.sizeThreshold {
c.warn(rng, size)
}
}
Expand Down
2 changes: 1 addition & 1 deletion checkers/rangeValCopy_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *rangeValCopyChecker) VisitStmt(stmt ast.Stmt) {
if _, ok := typ.(*typeparams.TypeParam); ok {
return
}
if size := c.ctx.SizesInfo.Sizeof(typ); size >= c.sizeThreshold {
if size, ok := c.ctx.SizeOf(typ); ok && size >= c.sizeThreshold {
c.warn(rng, size)
}
}
Expand Down
10 changes: 8 additions & 2 deletions checkers/truncateCmp_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ func (c *truncateCmpChecker) checkCmp(cmpX, cmpY ast.Expr) {
return
}

xsize := c.ctx.SizesInfo.Sizeof(xtyp)
ysize := c.ctx.SizesInfo.Sizeof(ytyp)
xsize, ok := c.ctx.SizeOf(xtyp)
if !ok {
return
}
ysize, ok := c.ctx.SizeOf(ytyp)
if !ok {
return
}
if xsize <= ysize {
return
}
Expand Down
11 changes: 11 additions & 0 deletions framework/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/types"

"github.com/go-toolsmith/astfmt"
"golang.org/x/exp/typeparams"
)

// CheckerCollection provides additional information for a group of checkers.
Expand Down Expand Up @@ -314,6 +315,16 @@ func (ctx *CheckerContext) TypeOf(x ast.Expr) types.Type {
return UnknownType
}

// SizeOf returns the size of the typ in bytes.
//
// Unlike SizesInfo.SizeOf, it will not panic on generic types.
func (ctx *CheckerContext) SizeOf(typ types.Type) (int64, bool) {
if _, ok := typ.(*typeparams.TypeParam); ok {
return 0, false
}
return ctx.SizesInfo.Sizeof(typ), true
}

// FileWalker is an interface every checker should implement.
//
// The WalkFile method is executed for every Go file inside the
Expand Down
5 changes: 5 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ func nilSafeTypeOf(m dsl.Matcher) {
m.Match(`$_.ctx.TypesInfo.TypeOf($x)`).
Report(`use ctx.TypeOf($x) instead, it's nil-safe`)
}

func stdSizeof(m dsl.Matcher) {
m.Match(`$_.ctx.SizesInfo.Sizeof($_)`).
Report(`use ctx.SizeOf instead`)
}

0 comments on commit dfd27d0

Please sign in to comment.