From 7ec95cc09e07b42d348b9654a550930ff5df2c9d Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Mon, 25 Nov 2024 09:39:42 +0100 Subject: [PATCH] Rust: Minor refactor and add comment --- .../rust/elements/internal/VariableImpl.qll | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll index 3f83594cfa86..734ee551a804 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll @@ -147,7 +147,9 @@ module Impl { predicate isCaptured() { this.getAnAccess().isCapture() } /** Gets the parameter that introduces this variable, if any. */ - Param getParameter() { parameterDeclInScope(result, this, _) } + AstNode getParameter() { + result = this.getSelfParam() or result = getAVariablePatAncestor(this).getParentNode() + } /** Hold is this variable is mutable. */ predicate isMutable() { this.getPat().isMut() } @@ -156,7 +158,11 @@ module Impl { predicate isImmutable() { not this.isMutable() } } - /** A path expression that may access a local variable. */ + /** + * A path expression that may access a local variable. These are paths that + * only consists of a simple name (i.e., without generic arguments, + * qualifiers, etc.). + */ private class VariableAccessCand extends PathExprBase { string name_; @@ -202,10 +208,7 @@ module Impl { private VariableScope getEnclosingScope(AstNode n) { result = getAnAncestorInVariableScope(n) } private Pat getAVariablePatAncestor(Variable v) { - exists(AstNode definingNode, string name | - v = MkVariable(definingNode, name) and - variableDecl(definingNode, result, name) - ) + result = v.getPat() or exists(Pat mid | mid = getAVariablePatAncestor(v) and @@ -217,13 +220,9 @@ module Impl { * Holds if parameter `p` introduces the variable `v` inside variable scope * `scope`. */ - private predicate parameterDeclInScope(AstNode p, Variable v, VariableScope scope) { + private predicate parameterDeclInScope(Variable v, VariableScope scope) { exists(Callable f | - ( - p = f.getParamList().getSelfParam() and p = v.getSelfParam() - or - p = f.getParamList().getAParam() and p.(Param).getPat() = getAVariablePatAncestor(v) - ) and + v.getParameter() = [f.getParamList().getSelfParam().(AstNode), f.getParamList().getAParam()] and scope = [f.(Function).getBody(), f.(ClosureExpr).getBody()] ) } @@ -237,7 +236,7 @@ module Impl { ) { name = v.getName() and ( - parameterDeclInScope(_, v, scope) and + parameterDeclInScope(v, scope) and scope.getLocation().hasLocationFileInfo(_, line, column, _, _) or exists(Pat pat | pat = getAVariablePatAncestor(v) |