Skip to content

How to list methods not wrapped in a certain statement? #5282

Answered by aeisenberg
markpahulje asked this question in Q&A
Discussion options

You must be logged in to vote

I'm assuming this is in C#. Something like this should work to find method calls not lexically contained by a try-catch.

import csharp

predicate inTryCatch(MethodCall mc) {
  exists(TryStmt ts | ts = mc.getParent*())
    or exists(CatchClause cc | cc = mc.getParent*())
}

from MethodCall mc
where not inTryCatch(mc)
select mc

Of course, this only finds method calls lexically enclosed. Are you looking for method calls that are part of a data flow that is not in a try-catch block? For that you will need to use data-flow analysis.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
1 reply
@markpahulje
Comment options

Answer selected by aeisenberg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants