Skip to content

Commit cffc933

Browse files
committed
rules/sdk: exclude "testutil" and other packages from map ranging checks
This change excludes: * "gogoreflection" * "simapp" * "simulation" * "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
1 parent 8d0d8e0 commit cffc933

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

rules/sdk/iterate_over_maps.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,24 @@ func (mr *mapRanging) ID() string {
3737
return mr.MetaData.ID
3838
}
3939

40+
// There are some packages that inherently need map ranging such as "testutil"
41+
// so return true if we detect such.
42+
func pkgExcusedFromMapRangingChecks(ctx *gosec.Context) bool {
43+
switch pkg := ctx.Pkg.Name(); pkg {
44+
case "gogoreflection", "simapp", "simulation", "testutil":
45+
return true
46+
default:
47+
return false
48+
}
49+
}
50+
4051
func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
52+
if pkgExcusedFromMapRangingChecks(ctx) {
53+
// Do nothing for such packages like "testutil".
54+
// Please see https://github.com/cosmos/gosec/issues/50
55+
return nil, nil
56+
}
57+
4158
rangeStmt, ok := node.(*ast.RangeStmt)
4259
if !ok {
4360
return nil, nil

0 commit comments

Comments
 (0)