Skip to content

Commit

Permalink
Merge pull request #17803 from paldepind/unreachable2
Browse files Browse the repository at this point in the history
Rust: More test cases for rust/dead-code
  • Loading branch information
paldepind authored Oct 21, 2024
2 parents 74ef916 + 4ddc7a4 commit e149071
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 16 deletions.
35 changes: 21 additions & 14 deletions rust/ql/test/query-tests/unusedentities/UnreachableCode.expected
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
| unreachable.rs:12:3:12:17 | ExprStmt | This code is never reached. |
| unreachable.rs:20:3:20:17 | ExprStmt | This code is never reached. |
| unreachable.rs:32:3:32:17 | ExprStmt | This code is never reached. |
| unreachable.rs:39:3:39:17 | ExprStmt | This code is never reached. |
| unreachable.rs:60:2:60:16 | ExprStmt | This code is never reached. |
| unreachable.rs:106:16:106:23 | ExprStmt | This code is never reached. |
| unreachable.rs:114:15:114:22 | ExprStmt | This code is never reached. |
| unreachable.rs:130:2:130:16 | ExprStmt | This code is never reached. |
| unreachable.rs:140:2:140:16 | ExprStmt | This code is never reached. |
| unreachable.rs:147:3:147:17 | ExprStmt | This code is never reached. |
| unreachable.rs:156:4:156:18 | ExprStmt | This code is never reached. |
| unreachable.rs:162:3:162:17 | ExprStmt | This code is never reached. |
| unreachable.rs:168:4:168:18 | ExprStmt | This code is never reached. |
| unreachable.rs:171:2:171:16 | ExprStmt | This code is never reached. |
| unreachable.rs:13:3:13:17 | ExprStmt | This code is never reached. |
| unreachable.rs:21:3:21:17 | ExprStmt | This code is never reached. |
| unreachable.rs:33:3:33:17 | ExprStmt | This code is never reached. |
| unreachable.rs:40:3:40:17 | ExprStmt | This code is never reached. |
| unreachable.rs:61:2:61:16 | ExprStmt | This code is never reached. |
| unreachable.rs:107:16:107:23 | ExprStmt | This code is never reached. |
| unreachable.rs:115:15:115:22 | ExprStmt | This code is never reached. |
| unreachable.rs:131:2:131:16 | ExprStmt | This code is never reached. |
| unreachable.rs:141:2:141:16 | ExprStmt | This code is never reached. |
| unreachable.rs:148:3:148:17 | ExprStmt | This code is never reached. |
| unreachable.rs:157:4:157:18 | ExprStmt | This code is never reached. |
| unreachable.rs:163:3:163:17 | ExprStmt | This code is never reached. |
| unreachable.rs:169:4:169:18 | ExprStmt | This code is never reached. |
| unreachable.rs:177:4:177:18 | ExprStmt | This code is never reached. |
| unreachable.rs:180:2:180:16 | ExprStmt | This code is never reached. |
| unreachable.rs:197:2:197:16 | ExprStmt | This code is never reached. |
| unreachable.rs:203:3:203:17 | ExprStmt | This code is never reached. |
| unreachable.rs:206:2:206:16 | ExprStmt | This code is never reached. |
| unreachable.rs:218:3:218:17 | ExprStmt | This code is never reached. |
| unreachable.rs:233:2:233:16 | ExprStmt | This code is never reached. |
| unreachable.rs:242:2:242:16 | ExprStmt | This code is never reached. |
5 changes: 5 additions & 0 deletions rust/ql/test/query-tests/unusedentities/UnusedValue.expected
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@
| more.rs:80:9:80:14 | a_ptr4 | Variable is assigned a value that is never used. |
| more.rs:95:9:95:13 | d_ptr | Variable is assigned a value that is never used. |
| more.rs:101:9:101:17 | f_ptr | Variable is assigned a value that is never used. |
| unreachable.rs:166:6:166:6 | x | Variable is assigned a value that is never used. |
| unreachable.rs:190:14:190:14 | a | Variable is assigned a value that is never used. |
| unreachable.rs:199:9:199:9 | a | Variable is assigned a value that is never used. |
| unreachable.rs:210:11:210:11 | a | Variable is assigned a value that is never used. |
| unreachable.rs:217:6:217:6 | a | Variable is assigned a value that is never used. |
7 changes: 6 additions & 1 deletion rust/ql/test/query-tests/unusedentities/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,14 @@ fn main() {
func_ptrs();
folds_and_closures();

unreachable_if();
unreachable_if_1();
unreachable_panic();
unreachable_match();
unreachable_loop();
unreachable_paren();
unreachable_let_1();
unreachable_let_2();
unreachable_if_2();
unreachable_if_3();

}
67 changes: 66 additions & 1 deletion rust/ql/test/query-tests/unusedentities/unreachable.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

//fn cond() -> bool;
//fn get_a_number() -> i32;
//fn maybe_get_a_number() -> Option<i32>;

// --- unreachable code --

fn do_something() {
}

fn unreachable_if() {
fn unreachable_if_1() {
if false {
do_something(); // BAD: unreachable code
} else {
Expand Down Expand Up @@ -162,6 +163,14 @@ fn unreachable_loop() {
do_something(); // BAD: unreachable code
}

for x in 1..10 {
if cond() {
continue;
do_something(); // BAD: unreachable code
}
do_something();
}

loop {
if cond() {
return;
Expand All @@ -176,3 +185,59 @@ fn unreachable_loop() {
fn unreachable_paren() {
let _ = (((1)));
}

fn unreachable_let_1() {
if let Some(a) = maybe_get_a_number() {
do_something();
return;
} else {
do_something();
}

do_something(); // SPURIOUS: unreachable code

if let a = get_a_number() { // (always succeeds)
do_something();
return;
} else {
do_something(); // BAD: unreachable code
}

do_something(); // BAD: unreachable code
}

fn unreachable_let_2() {
let Some(a) = maybe_get_a_number() else {
do_something();
return;
};

do_something();

let a = maybe_get_a_number() else { // (always succeeds)
do_something(); // BAD: unreachable code
return;
};

do_something();
}

fn unreachable_if_2() {
if cond() {
do_something();
return;
} else {
do_something();
}

do_something(); // SPURIOUS: unreachable code
}

fn unreachable_if_3() {
if !cond() {
do_something();
return;
}

do_something(); // SPURIOUS: unreachable code
}

0 comments on commit e149071

Please sign in to comment.