Why doesn't allow unreachable code work here

It's definitely not a big deal. But I want to understand this. I thought placing #[allow(unreachable_code)] would stop the warning

26 |     println!("after exit");
   |     ^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

The lines in the code are

    std::process::exit(0);

    #[allow(unreachable_code)]
    println!("after exit");

My understanding was that if I placed allow unreachable_code above some lines of code the unreachable code warning for those lines would be suppressed. Can anyone please help me understand this? thanks,

It's likely due to the fact that println! expands to multiple statements, and allow is applied only to the first of them. By wrapping the macro in block, so that it will be one statement, I've got rid of warning - playground:

fn main() {
    return;
    #[allow(unreachable_code)]
    {
        println!("text");
    }
}
2 Likes

Thanks. Adding the braces worked.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.