Skip to content

Commit

Permalink
rules/sdk: exclude "testutil" from map ranging checks
Browse files Browse the repository at this point in the history
This change excludes "testutil" from map ranging checks
given that such code is used for testing and no need to flag
natural code to iterate over tests.

Fixes #50
  • Loading branch information
odeke-em committed Sep 24, 2022
1 parent 8d0d8e0 commit f404ef7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rules/sdk/iterate_over_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ func (mr *mapRanging) ID() string {
return mr.MetaData.ID
}

// There are some packages that inherently need map ranging such as "testutil"
// so return true if we detect such.
func pkgExcusedFromMapRangingChecks(ctx *gosec.Context) bool {
switch pkg := ctx.Pkg.Name(); pkg {
case "testutil":
return true
default:
return false
}
}

func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
if pkgExcusedFromMapRangingChecks(ctx) {
// Do nothing for such packages like "testutil".
// Please see https://github.com/cosmos/gosec/issues/50
return nil, nil
}

rangeStmt, ok := node.(*ast.RangeStmt)
if !ok {
return nil, nil
Expand Down

0 comments on commit f404ef7

Please sign in to comment.