Skip to content

Commit

Permalink
Merge pull request #5 from hvitved/unusedvar2
Browse files Browse the repository at this point in the history
Rust: Account for variables bound in `while let` expressions
  • Loading branch information
geoffw0 authored Oct 3, 2024
2 parents ccaf2dd + cd04500 commit 369241e
Show file tree
Hide file tree
Showing 6 changed files with 676 additions and 636 deletions.
7 changes: 7 additions & 0 deletions rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ module Impl {
scope = ce.getBody() and
scope.getLocation().hasLocationInfo(_, line, column, _, _)
)
or
exists(WhileExpr we, LetExpr let |
let.getPat() = pat and
we.getCondition() = let and
scope = we.getLoopBody() and
scope.getLocation().hasLocationInfo(_, line, column, _, _)
)
)
}

Expand Down
885 changes: 452 additions & 433 deletions rust/ql/test/library-tests/variables/Cfg.expected

Large diffs are not rendered by default.

369 changes: 188 additions & 181 deletions rust/ql/test/library-tests/variables/variables.expected

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions rust/ql/test/library-tests/variables/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ fn let_pattern4() {
print_str(x5); // $ access=x5
}

fn let_pattern5() {
let s1 = Some(String::from("Hello!")); // s1

while let Some(ref s2) // s2
= s1 { // $ access=s1
print_str(s2); // $ access=s2
}
}

fn match_pattern1() {
let x6 = Some(5); // x6
let y1 = 10; // y1_1
Expand Down
15 changes: 7 additions & 8 deletions rust/ql/test/query-tests/unusedentities/UnusedVariable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
| main.rs:114:9:114:9 | k | Variable is not used. |
| main.rs:141:5:141:5 | y | Variable is not used. |
| main.rs:164:9:164:9 | x | Variable is not used. |
| main.rs:170:9:170:9 | x | Variable is not used. |
| main.rs:169:9:169:9 | x | Variable is not used. |
| main.rs:174:9:174:9 | x | Variable is not used. |
| main.rs:194:17:194:17 | a | Variable is not used. |
| main.rs:202:20:202:22 | val | Variable is not used. |
| main.rs:207:20:207:22 | val | Variable is not used. |
| main.rs:214:14:214:16 | val | Variable is not used. |
| main.rs:216:9:216:12 | None | Variable is not used. |
| main.rs:225:9:225:12 | None | Variable is not used. |
| main.rs:231:24:231:26 | val | Variable is not used. |
| main.rs:195:17:195:17 | a | Variable is not used. |
| main.rs:203:20:203:22 | val | Variable is not used. |
| main.rs:216:14:216:16 | val | Variable is not used. |
| main.rs:218:9:218:12 | None | Variable is not used. |
| main.rs:227:9:227:12 | None | Variable is not used. |
| main.rs:233:24:233:26 | val | Variable is not used. |
27 changes: 13 additions & 14 deletions rust/ql/test/query-tests/unusedentities/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ fn loops() {
for x in 1..10 { // BAD: unused variable
}

for _ in 1..10 {
}
for _ in 1..10 {}

for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
for x // SPURIOUS: unused variable [macros not yet supported]
in 1..10 {
println!("x is {}", x);
}

for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
for x // SPURIOUS: unused variable [macros not yet supported]
in 1..10 {
assert!(x != 11);
}
}
Expand Down Expand Up @@ -199,20 +200,21 @@ fn if_lets() {
}

let mut next = Some(30);
while let Some(val) = next { // BAD: unused variable
while let Some(val) = next // BAD: unused variable
{
next = None;
}

let mut next2 = Some(40);
while let Some(val) = next2 { // SPURIOUS: unused variable 'val'
while let Some(val) = next2 {
total += val;
next2 = None;
}

let c = Some(60);
match c {
Some(val) => { // BAD: unused variable
},
}
None => { // SPURIOUS: unused variable 'None'
}
}
Expand All @@ -221,25 +223,22 @@ fn if_lets() {
match d {
Some(val) => {
total += val;
},
}
None => { // SPURIOUS: unused variable 'None'
}
}

let e = MyOption::Some(80);
match e {
MyOption::Some(val) => { // BAD: unused variable
},
MyOption::None => {
}
MyOption::None => {}
}

let f = YesOrNo::Yes;
match f {
YesOrNo::Yes => {
},
YesOrNo::No => {
},
YesOrNo::Yes => {}
YesOrNo::No => {}
}
}

Expand Down

0 comments on commit 369241e

Please sign in to comment.