Skip to content

Commit 526620c

Browse files
committed
Rust: Add some helper predicates for finding enclosing blocks.
1 parent 96dc34e commit 526620c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ module Impl {
5959
)
6060
}
6161

62+
/** Gets the block that encloses this node, if any. */
63+
cached
64+
BlockExpr getEnclosingBlock() {
65+
exists(AstNode p | p = this.getParentNode() |
66+
result = p
67+
or
68+
not p instanceof BlockExpr and
69+
result = p.getEnclosingBlock()
70+
)
71+
}
72+
6273
/** Holds if this node is inside a macro expansion. */
6374
predicate isInMacroExpansion() { MacroCallImpl::isInMacroExpansion(_, this) }
6475

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ module Impl {
127127
*/
128128
Name getName() { variableDecl(definingNode, result, text) }
129129

130+
/** Gets the block that encloses this variable, if any. */
131+
cached
132+
BlockExpr getEnclosingBlock() { result = definingNode.getEnclosingBlock() }
133+
130134
/** Gets the `self` parameter that declares this variable, if any. */
131135
SelfParam getSelfParam() { result.getName() = this.getName() }
132136

0 commit comments

Comments
 (0)