Skip to content

Commit e1c315b

Browse files
committed
Rust: make ImplicitVariableAccess instance of VariableAccess
1 parent 1b7b24e commit e1c315b

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import rust
22
private import codeql.rust.elements.internal.generated.ParentChild
3-
private import codeql.rust.elements.internal.PathExprImpl::Impl as PathExprImpl
3+
private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
44
private import codeql.util.DenseRank
55

66
module Impl {
@@ -138,12 +138,12 @@ module Impl {
138138
}
139139

140140
/** A path expression that may access a local variable. */
141-
private class VariableAccessCand extends PathExpr {
141+
private class VariableAccessCand extends TVariableAccess {
142142
string name_;
143143

144144
VariableAccessCand() {
145145
exists(Path p, PathSegment ps |
146-
p = this.getPath() and
146+
p = this.(PathExpr).getPath() and
147147
not p.hasQualifier() and
148148
ps = p.getPart() and
149149
not ps.hasGenericArgList() and
@@ -152,9 +152,23 @@ module Impl {
152152
not ps.hasReturnTypeSyntax() and
153153
name_ = ps.getNameRef().getText()
154154
)
155+
or
156+
this.(ImplicitVariableAccess).getName() = name_
155157
}
156158

159+
string toString() { result = name_ }
160+
157161
string getName() { result = name_ }
162+
163+
predicate hasLocationInfo(
164+
string filepath, int startline, int startcolumn, int endline, int endcolumn
165+
) {
166+
this.(PathExpr)
167+
.getLocation()
168+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) or
169+
this.(ImplicitVariableAccess)
170+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
171+
}
158172
}
159173

160174
private AstNode getAnAncestorInVariableScope(AstNode n) {
@@ -164,7 +178,10 @@ module Impl {
164178
n instanceof LetStmt or
165179
n instanceof VariableScope
166180
) and
167-
exists(AstNode n0 | result = getImmediateParent(n0) |
181+
exists(AstNode n0 |
182+
result = getImmediateParent(n0) or
183+
result = n0.(ImplicitVariableAccess).getArgument().getParent().getParent()
184+
|
168185
n0 = n
169186
or
170187
n0 = getAnAncestorInVariableScope(n) and
@@ -272,7 +289,7 @@ module Impl {
272289
) {
273290
name = cand.getName() and
274291
scope = [cand.(VariableScope), getEnclosingScope(cand)] and
275-
cand.getLocation().hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
292+
cand.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
276293
nestLevel = 0
277294
or
278295
exists(VariableScope inner |
@@ -347,8 +364,14 @@ module Impl {
347364
result = this.asVariable().toString() or result = this.asVariableAccessCand().toString()
348365
}
349366

350-
Location getLocation() {
351-
result = this.asVariable().getLocation() or result = this.asVariableAccessCand().getLocation()
367+
predicate hasLocationInfo(
368+
string filepath, int startline, int startcolumn, int endline, int endcolumn
369+
) {
370+
this.asVariable()
371+
.getLocation()
372+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) or
373+
this.asVariableAccessCand()
374+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
352375
}
353376

354377
pragma[nomagic]
@@ -421,8 +444,12 @@ module Impl {
421444
)
422445
}
423446

447+
private import codeql.rust.elements.internal.generated.Synth
448+
449+
private class TVariableAccess = Synth::TPathExpr or Synth::TImplicitVariableAccess;
450+
424451
/** A variable access. */
425-
class VariableAccess extends PathExprImpl::PathExpr instanceof VariableAccessCand {
452+
class VariableAccess extends ExprImpl::Expr, TVariableAccess instanceof VariableAccessCand {
426453
private string name;
427454
private Variable v;
428455

0 commit comments

Comments
 (0)