I have a variable that is set as a global variable in the package (not inside a function):
var myArr []byte = make([]byte, 0)
I wanted to find all the places where a variable is initialized with make([]byte, 0)
.
This is what I tried to do:
from AssignStmt assign, Ident id, CallExpr make
where
assign.getLhs().(Ident) = id and
make = assign.getRhs().(CallExpr) and
make.getTarget() = Builtin::make() and
make.getArgument(0).getType().(ArrayType).getElementType().hasQualifiedName("", "byte") and
make.getArgument(1).getIntValue() = 0
select assign, id, "This variable is assigned with make([]byte, 0)."
But it doesn't find anything although I have such lines in the code.
Based on the documentation it seems that I need to use DeclStmt
to search for a declared variable. I am still trying to figure out how.
Credit to Owen Mansel-Chan for his help. They have a Slack that I am helping with but thought maybe someone here would have an answer.
Owen Mansel-Chan answered me on the Slack:
from VarDecl vd, ValueSpec vs, CallExpr rhs
where
vs = vd.getASpec() and
vs.getNumInit() = 1 and
rhs = vs.getInit(0) and
rhs.getTarget() = Builtin::make() and
rhs.getArgument(0).getType().(SliceType).getElementType() = Builtin::byte().getType() and
rhs.getArgument(1).getIntValue() = 0 and
not exists(rhs.getEnclosingFunction())
select vd, vs, rhs
I checked and it works.