1
0
mirror of https://github.com/securego/gosec.git synced 2025-12-24 00:01:46 +02:00

Add match call by type

This commit is contained in:
Grant Murphy
2016-11-17 20:18:31 -08:00
parent d30c5cde36
commit d29c64800e
2 changed files with 86 additions and 0 deletions

View File

@@ -88,6 +88,33 @@ func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*a
return nil, false
}
// MatchCallByType ensures that the node is a call expression to a
// specific object type.
//
// Usage:
// node, matched := MatchCallByType(n, ctx, "bytes.Buffer", "WriteTo", "Write")
//
func MatchCallByType(n ast.Node, ctx *Context, requiredType string, calls ...string) (*ast.CallExpr, bool) {
switch callExpr := n.(type) {
case *ast.CallExpr:
switch fn := callExpr.Fun.(type) {
case *ast.SelectorExpr:
switch expr := fn.X.(type) {
case *ast.Ident:
t := ctx.Info.TypeOf(expr)
if t != nil && t.String() == requiredType {
for _, call := range calls {
if fn.Sel.Name == call {
return callExpr, true
}
}
}
}
}
}
return nil, false
}
// MatchCompLit will match an ast.CompositeLit if its string value obays the given regex.
func MatchCompLit(n ast.Node, r *regexp.Regexp) *ast.CompositeLit {
t := reflect.TypeOf(&ast.CompositeLit{})