quasilyte/go-ruleguard

Ability to match on a non-deferred function call only

nvx opened this issue · 2 comments

nvx commented

It's pretty easy to match on a particular function being called, and it's also easy to match when a function is called via defer, but doesn't seem possible is to match a function is being called non-deferred only.

For example it should match this:
foo.Bar()
but not match this:
defer foo.Bar()

Is this currently able to be expressed with ruleguard? I haven't been able to find any examples of such a use so far.

I would try something like this:

m.Match(`foo($*_)`).
  Where(!m[`$$`].Parent().Is(`DeferStmt`)).
  Report(...)

Is that good enough?
It should report every foo call except for the ones that are called via defer.

nvx commented

I would try something like this:

m.Match(`foo($*_)`).
  Where(!m[`$$`].Parent().Is(`DeferStmt`)).
  Report(...)

Is that good enough? It should report every foo call except for the ones that are called via defer.

That's exactly what I was after, with the slight correction of needing .Node before Parent() (without this I got a syntax error), ie Where(!m["$$"].Node.Parent().Is("DeferStmt")).

Cheers!