@@ -35,6 +35,22 @@ func (mr *mapRanging) ID() string {
35
35
return mr .MetaData .ID
36
36
}
37
37
38
+ func extractIdent (call ast.Expr ) * ast.Ident {
39
+ switch n := call .(type ) {
40
+ case * ast.Ident :
41
+ return n
42
+
43
+ case * ast.SelectorExpr :
44
+ if ident , ok := n .X .(* ast.Ident ); ok {
45
+ return ident
46
+ }
47
+ return extractIdent (n .X )
48
+
49
+ default :
50
+ panic (fmt .Sprintf ("Unhandled type: %T" , call ))
51
+ }
52
+ }
53
+
38
54
func (mr * mapRanging ) Match (node ast.Node , ctx * gosec.Context ) (* gosec.Issue , error ) {
39
55
rangeStmt , ok := node .(* ast.RangeStmt )
40
56
if ! ok {
@@ -61,7 +77,7 @@ func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, er
61
77
case * ast.CallExpr :
62
78
// Synthesize the declaration to be an *ast.FuncType from
63
79
// either function declarations or function literals.
64
- idecl := rangeRHS .Fun .( * ast. Ident ).Obj .Decl
80
+ idecl := extractIdent ( rangeRHS .Fun ).Obj .Decl
65
81
switch idecl := idecl .(type ) {
66
82
case * ast.FuncDecl :
67
83
decl = idecl .Type
@@ -83,8 +99,7 @@ func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, er
83
99
}
84
100
85
101
case * ast.AssignStmt :
86
- rhs0 := decl .Rhs [0 ].(* ast.CompositeLit )
87
- if _ , ok := rhs0 .Type .(* ast.MapType ); ! ok {
102
+ if skip := mapHandleAssignStmt (decl ); skip {
88
103
return nil , nil
89
104
}
90
105
@@ -154,6 +169,23 @@ func (mr *mapRanging) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, er
154
169
}
155
170
}
156
171
172
+ func mapHandleAssignStmt (decl * ast.AssignStmt ) (skip bool ) {
173
+ switch rhs0 := decl .Rhs [0 ].(type ) {
174
+ case * ast.CompositeLit :
175
+ if _ , ok := rhs0 .Type .(* ast.MapType ); ! ok {
176
+ return true
177
+ }
178
+ return false
179
+
180
+ case * ast.CallExpr :
181
+ return true
182
+
183
+ default :
184
+ // TODO: handle other types.
185
+ return true
186
+ }
187
+ }
188
+
157
189
func eitherAppendOrDeleteCall (callExpr * ast.CallExpr ) (string , bool ) {
158
190
fn , ok := callExpr .Fun .(* ast.Ident )
159
191
if ! ok {
@@ -187,6 +219,13 @@ func typeOf(value interface{}) ast.Node {
187
219
return decl .Args [0 ]
188
220
}
189
221
return typeOf (decl .Args [0 ])
222
+
223
+ case * ast.AssignStmt :
224
+ ident , ok := typ .Lhs [0 ].(* ast.Ident )
225
+ if ok {
226
+ return ident
227
+ }
228
+ return typeOf (typ .Lhs [0 ])
190
229
}
191
230
192
231
panic (fmt .Sprintf ("Unexpected type: %T" , value ))
0 commit comments