diff --git a/go/ql/lib/semmle/go/AST.qll b/go/ql/lib/semmle/go/AST.qll index c5e388ba000f..6a840f81cbbe 100644 --- a/go/ql/lib/semmle/go/AST.qll +++ b/go/ql/lib/semmle/go/AST.qll @@ -84,6 +84,16 @@ class AstNode extends @node, Locatable { pragma[nomagic] FuncDef getEnclosingFunction() { result = this.getParent().parentInSameFunction*() } + /** Gets the innermost block statement to which this AST node belongs, if any. */ + BlockStmt getEnclosingBlock() { + exists(AstNode p | p = this.getParent() | + result = p + or + not p instanceof BlockStmt and + result = p.getEnclosingBlock() + ) + } + /** * Gets a comma-separated list of the names of the primary CodeQL classes to which this element belongs. */