Skip to content

Commit

Permalink
Rust: Minor refactor and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Nov 25, 2024
1 parent 380ae4a commit 7ec95cc
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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_;

Expand Down Expand Up @@ -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
Expand All @@ -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()]
)
}
Expand All @@ -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) |
Expand Down

0 comments on commit 7ec95cc

Please sign in to comment.