Skip to content
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

warning: unreachable expression: unreachable!() #45414

Closed
crlf0710 opened this issue Oct 20, 2017 · 1 comment
Closed

warning: unreachable expression: unreachable!() #45414

crlf0710 opened this issue Oct 20, 2017 · 1 comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@crlf0710
Copy link
Member

Hi, i'm not really sure if this is a bug, but the following code:

fn foo () -> usize {
    loop {
        return 1;
    }
    unreachable!()
}

emits

warning: unreachable expression
 --> src/main.rs:5:5
  |
5 |     unreachable!()
  |     ^^^^^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default
  = note: this error originates in a macro outside of the current crate

I'm using a recent nightly and it works this way with 1.21 stable rustc too.

@bluss
Copy link
Member

bluss commented Oct 20, 2017

Hi, I don't think this is a bug — the compiler knows that code after the loop is unreachable. The compiler has warned the same way since (at least) Rust 1.0 for this. You can safely remove the unreachable!() macro.

If you find a warning that doesn't make sense, or where following the logical implication of the warning leads to code that doesn't compile, then please file a bug for that. Thanks!

@bluss bluss closed this as completed Oct 20, 2017
@bluss bluss added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Oct 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

2 participants