Skip to content

Commit

Permalink
Rust: Additional test cases for rust/dead-code.
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffw0 committed Nov 6, 2024
1 parent a8b1cb3 commit fbfdd57
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 23 deletions.
25 changes: 16 additions & 9 deletions rust/ql/test/query-tests/unusedentities/UnreachableCode.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
| unreachable.rs:67:5:67:19 | ExprStmt | This code is never reached. |
| unreachable.rs:134:13:134:20 | MacroExpr | This code is never reached. |
| unreachable.rs:143:13:143:20 | MacroExpr | This code is never reached. |
| unreachable.rs:169:5:169:19 | ExprStmt | This code is never reached. |
| unreachable.rs:177:13:177:27 | ExprStmt | This code is never reached. |
| unreachable.rs:187:13:187:27 | ExprStmt | This code is never reached. |
| unreachable.rs:193:9:193:23 | ExprStmt | This code is never reached. |
| unreachable.rs:200:17:200:31 | ExprStmt | This code is never reached. |
| unreachable.rs:210:17:210:31 | ExprStmt | This code is never reached. |
| unreachable.rs:213:9:213:23 | ExprStmt | This code is never reached. |
| unreachable.rs:238:9:238:23 | ExprStmt | This code is never reached. |
| unreachable.rs:254:9:254:23 | ExprStmt | This code is never reached. |
| unreachable.rs:166:9:166:23 | ExprStmt | This code is never reached. |
| unreachable.rs:171:9:171:17 | MacroExpr | This code is never reached. |
| unreachable.rs:177:9:177:26 | MacroExpr | This code is never reached. |
| unreachable.rs:206:9:206:23 | ExprStmt | This code is never reached. |
| unreachable.rs:231:13:231:27 | ExprStmt | This code is never reached. |
| unreachable.rs:241:13:241:27 | ExprStmt | This code is never reached. |
| unreachable.rs:247:9:247:23 | ExprStmt | This code is never reached. |
| unreachable.rs:254:17:254:31 | ExprStmt | This code is never reached. |
| unreachable.rs:264:17:264:31 | ExprStmt | This code is never reached. |
| unreachable.rs:267:9:267:23 | ExprStmt | This code is never reached. |
| unreachable.rs:303:9:303:23 | ExprStmt | This code is never reached. |
| unreachable.rs:332:9:332:23 | ExprStmt | This code is never reached. |
| unreachable.rs:348:9:348:23 | ExprStmt | This code is never reached. |
| unreachable.rs:370:9:370:23 | ExprStmt | This code is never reached. |
| unreachable.rs:375:5:375:10 | ExprStmt | This code is never reached. |
| unreachable.rs:382:5:382:10 | ExprStmt | This code is never reached. |
2 changes: 2 additions & 0 deletions rust/ql/test/query-tests/unusedentities/UnusedValue.expected
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
| more.rs:44:9:44:14 | a_ptr4 | Variable $@ is assigned a value that is never used. | more.rs:44:9:44:14 | a_ptr4 | a_ptr4 |
| more.rs:59:9:59:13 | d_ptr | Variable $@ is assigned a value that is never used. | more.rs:59:9:59:13 | d_ptr | d_ptr |
| more.rs:65:9:65:17 | f_ptr | Variable $@ is assigned a value that is never used. | more.rs:65:13:65:17 | f_ptr | f_ptr |
| unreachable.rs:292:9:292:15 | for_ten | Variable $@ is assigned a value that is never used. | unreachable.rs:292:9:292:15 | for_ten | for_ten |
| unreachable.rs:299:9:299:16 | for_ever | Variable $@ is assigned a value that is never used. | unreachable.rs:299:9:299:16 | for_ever | for_ever |
3 changes: 3 additions & 0 deletions rust/ql/test/query-tests/unusedentities/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,14 @@ fn main() {
unreachable_if_2();
unreachable_if_3();
unreachable_panic();
_ = unreachable_bail();
unreachable_match();
unreachable_loop();
unreachable_loop_async();
unreachable_paren();
unreachable_let_1();
unreachable_let_2();
unreachable_attributes();

macros();
}
152 changes: 138 additions & 14 deletions rust/ql/test/query-tests/unusedentities/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,80 @@ pub fn unreachable_panic() {
}
}

pub fn unreachable_match() {
match get_a_number() {
1 => {
return;
}
_ => {
do_something();
}
macro_rules! bail_1 {
() => {
return Err(String::from("message"))
};
}

macro_rules! bail_2 {
($message:literal) => {
return Err(String::from($message))
};
}

pub fn unreachable_bail() -> Result<i32, String> {
if cond() {
do_something();
return Err(String::from("message"));
do_something(); // $ Alert[rust/dead-code]
}

if cond() {
do_something();
bail_1!(); // $ SPURIOUS: Alert[rust/dead-code]
do_something(); // $ MISSING: Alert[rust/dead-code]
}

if cond() {
do_something();
bail_2!("message"); // $ SPURIOUS: Alert[rust/dead-code]
}
do_something();

match get_a_number() {
1 => {
return;
Ok(1)
}

pub fn unreachable_match() {
if cond() {
match get_a_number() {
1 => {
return;
}
_ => {
do_something();
}
}
_ => {
return;
do_something();
}

if cond() {
match get_a_number() {
1 => {
return;
}
_ => {
return;
}
}
do_something(); // $ Alert[rust/dead-code]
}

if cond() {
_ = match get_a_number() {
1 => "One",
_ => "Some"
};
do_something();
}

if cond() {
_ = Some(match get_a_number() {
1 => "One",
_ => "Some"
});
do_something();
}
do_something(); // $ Alert[rust/dead-code]
}

pub fn unreachable_loop() {
Expand Down Expand Up @@ -214,6 +268,46 @@ pub fn unreachable_loop() {
do_something();
do_something();
}

if cond() {
fn do_nothing() { };
fn loop_forever() { loop {} };
fn take_a_fn(_: fn() -> ()) {
};
fn call_a_fn(f: fn() -> ()) {
f();
};

take_a_fn( do_nothing );
call_a_fn( do_nothing );
take_a_fn( loop_forever );
call_a_fn( loop_forever );
do_something(); // $ MISSING: Alert[rust/dead-code]
}
}

async fn do_something_async() {}

pub async fn unreachable_loop_async() {
let for_ten = async { // $ SPURIOUS: Alert[rust/unused-value]
for _ in 1..10 {
do_something_async().await;
}
do_something();
};

let for_ever = async { // $ SPURIOUS: Alert[rust/unused-value]
loop {
do_something_async().await;
}
do_something(); // $ Alert[rust/dead-code]
};

do_something();
for_ten.await;
do_something();
for_ever.await;
do_something(); // $ MISSING: Alert[rust/dead-code]
}

pub fn unreachable_paren() {
Expand Down Expand Up @@ -257,3 +351,33 @@ pub fn unreachable_let_2() {

do_something();
}

#[cfg(not(foo))]
pub fn unreachable_attributes() {
// `#[cfg` and `cfg!` checks can go either way, we should not assume this
// function or either branch below is unreachable.
if cfg!(bar) {
do_something();
} else {
do_something();
}

#[doc="This is a doc comment declared through an attribute."]

if (true) {
do_something();
} else {
do_something(); // $ Alert[rust/dead-code]
}
}

const _: () = {
_ = 1; // $ SPURIOUS: Alert[rust/dead-code]
};

const _: () = {
const fn foo() {
_ = 1;
};
foo(); // $ SPURIOUS: Alert[rust/dead-code]
};

0 comments on commit fbfdd57

Please sign in to comment.