-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust: More tests for rust/deadcode #17923
base: main
Are you sure you want to change the base?
Changes from 3 commits
bf0e100
a8b1cb3
fbfdd57
3805d0f
c7112ef
727a7d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,26 @@ pub fn unreachable_if_1() { | |
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
|
||
pub fn unreachable_if_2() { | ||
if cond() { | ||
do_something(); | ||
return; | ||
} else { | ||
do_something(); | ||
} | ||
|
||
do_something(); | ||
} | ||
|
||
pub fn unreachable_if_3() { | ||
if !cond() { | ||
do_something(); | ||
return; | ||
} | ||
|
||
do_something(); | ||
} | ||
|
||
pub fn unreachable_panic() { | ||
if cond() { | ||
do_something(); | ||
|
@@ -127,33 +147,89 @@ 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() { | ||
loop { | ||
do_something(); | ||
break; | ||
do_something(); // $ Alert[rust/dead-code] | ||
if cond() { | ||
loop { | ||
do_something(); | ||
break; | ||
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
} | ||
|
||
if cond() { | ||
|
@@ -171,23 +247,67 @@ pub fn unreachable_loop() { | |
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
|
||
for _ in 1..10 { | ||
if cond() { | ||
continue; | ||
do_something(); // $ Alert[rust/dead-code] | ||
if cond() { | ||
for _ in 1..10 { | ||
if cond() { | ||
continue; | ||
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
do_something(); | ||
} | ||
do_something(); | ||
} | ||
|
||
loop { | ||
if cond() { | ||
return; | ||
do_something(); // $ Alert[rust/dead-code] | ||
if cond() { | ||
loop { | ||
if cond() { | ||
return; | ||
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
} | ||
do_something(); // $ Alert[rust/dead-code] | ||
do_something(); | ||
do_something(); | ||
} | ||
do_something(); // $ Alert[rust/dead-code] | ||
|
||
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() { | ||
|
@@ -232,22 +352,32 @@ pub fn unreachable_let_2() { | |
do_something(); | ||
} | ||
|
||
pub fn unreachable_if_2() { | ||
if cond() { | ||
#[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(); | ||
return; | ||
} else { | ||
do_something(); | ||
} | ||
|
||
do_something(); | ||
} | ||
#[doc="This is a doc comment declared through an attribute."] | ||
|
||
pub fn unreachable_if_3() { | ||
if !cond() { | ||
if (true) { | ||
do_something(); | ||
return; | ||
} else { | ||
do_something(); // $ Alert[rust/dead-code] | ||
} | ||
|
||
do_something(); | ||
} | ||
|
||
const _: () = { | ||
_ = 1; // $ SPURIOUS: Alert[rust/dead-code] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a child of a Const ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One argument for including them in the CFG is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know what steps we need to take to include them in the CFG? Is it just a case of adding the entry points somewhere??? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've created an issue to track this, but I could do with some pointers (or someone else to take on the issue, if that is easier). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I'll be looking at that together as well as the |
||
}; | ||
|
||
const _: () = { | ||
const fn foo() { | ||
_ = 1; | ||
}; | ||
foo(); // $ SPURIOUS: Alert[rust/dead-code] | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an issue with the workarounds for
PostOrderTree
and macro nodes. I'll figure out a way to fix it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created an issue to track this, I know what to do but I have other priorities.