Skip to content

unreachable_code detection and/or lint seems broken #126808

Open
@correabuscar

Description

@correabuscar

I tried this code:
playground

fn foo(s:&str) {
    println!("{}",s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    { // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }
    //No warnings if you comment these out:
    #[allow(unreachable_code)]
    foo("Hello, world!");
}

I expected to see this happen: no warnings

Instead, this happened: seeing unreachable code warning while the lint to allow it implies that it shouldn't warn!

 Compiling playground v0.0.1 (/playground)
warning: unreachable statement
  --> src/main.rs:7:5
   |
5  |       panic!("foo");
   |       ------------- any code following this expression is unreachable
6  |       #[allow(unreachable_code)]
7  | /     { // Warning points to this block
8  | |         foo("oh hi");
9  | |         foo("Hello, world!");
10 | |     }
   | |_____^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s

Now look what happens if nothing else follows:
playground

fn foo(s:&str) {
    println!("{}",s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    { // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }
    //No warnings if you comment these out:
    //#[allow(unreachable_code)]
    //foo("Hello, world!");
}

output:

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
     Running `target/debug/playground`

that's right, no warnings!
But since the warning was for the block above, why would changing the next statements affect it?!

Am I missing something? or something seems very broken.

If I put a comma after the block, same issue:
playground

fn foo(s: &str) {
    println!("{}", s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    {
        // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }; //remove this comma to fix it
}

output:

Compiling playground v0.0.1 (/playground)
warning: unreachable statement
  --> src/main.rs:7:5
   |
5  |       panic!("foo");
   |       ------------- any code following this expression is unreachable
6  |       #[allow(unreachable_code)]
7  | /     {
8  | |         // Warning points to this block
9  | |         foo("oh hi");
10 | |         foo("Hello, world!");
11 | |     }; //remove this comma to fix it
   | |______^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.62s

Remove the comma and no warnings again. But maybe this kinda makes sense with the comma, unclear.

Meta

rustc --version --verbose:
lastest nightly or stable on playground, or even rustc 1.78.0-nightly (9b00956e5 2024-04-29) (gentoo), or latest nightly on gentoo:

$ rustv
rustc 1.81.0-nightly (684b3553f 2024-06-20)
binary: rustc
commit-hash: 684b3553f70148ded97a80371c2405984d4f6aa7
commit-date: 2024-06-20
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
cargo 1.81.0-nightly (3ed207e41 2024-06-18)
release: 1.81.0-nightly
commit-hash: 3ed207e416fb2f678a40cc79c02dcf4f936a21ce
commit-date: 2024-06-18
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Gentoo Linux 2.15.0 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unreachable_codeLint: unreachable_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions