Skip to content

Commit 3a1f6ef

Browse files
committed
Address review comments
1 parent fb9ec24 commit 3a1f6ef

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -387,27 +387,36 @@ module Impl {
387387
override string getAPrimaryQlClass() { result = "VariableAccess" }
388388
}
389389

390-
/** Holds if `e` occurs in the LHS of a (compound) assignment. */
391-
private predicate assignLhs(Expr e) {
392-
exists(BinaryExpr be |
393-
be.getOperatorName().regexpMatch(".*=") and
390+
/** Holds if `e` occurs in the LHS of an assignment or compound assignment. */
391+
private predicate assignLhs(Expr e, boolean compound) {
392+
exists(BinaryExpr be, string op |
393+
op = be.getOperatorName().regexpCapture("(.*)=", 1) and
394394
e = be.getLhs()
395+
|
396+
op = "" and compound = false
397+
or
398+
op != "" and compound = true
395399
)
396400
or
397401
exists(Expr mid |
398-
assignLhs(mid) and
402+
assignLhs(mid, compound) and
399403
getImmediateParent(e) = mid
400404
)
401405
}
402406

403407
/** A variable write. */
404408
class VariableWriteAccess extends VariableAccess {
405-
VariableWriteAccess() { assignLhs(this) }
409+
VariableWriteAccess() { assignLhs(this, _) }
406410
}
407411

408412
/** A variable read. */
409413
class VariableReadAccess extends VariableAccess {
410-
VariableReadAccess() { not this instanceof VariableWriteAccess }
414+
VariableReadAccess() {
415+
not this instanceof VariableWriteAccess
416+
or
417+
// consider LHS in compound assignments both reads and writes
418+
assignLhs(this, true)
419+
}
411420
}
412421

413422
cached

rust/ql/test/query-tests/unusedentities/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn arrays() {
109109

110110
println!("lets use {:?}", js);
111111

112-
for k // BAD: unused variable [SPURIOUS: macros not yet supported]
112+
for k // SPURIOUS: unused variable [macros not yet supported]
113113
in ks
114114
{
115115
println!("lets use {}", k);

0 commit comments

Comments
 (0)