Skip to content

Misleading unreachable code warning from return in try blocks #54165

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
scottmcm opened this issue Sep 12, 2018 · 0 comments · Fixed by #64581
Closed

Misleading unreachable code warning from return in try blocks #54165

scottmcm opened this issue Sep 12, 2018 · 0 comments · Fixed by #64581
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-try_blocks `#![feature(try_blocks)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

#![feature(try_blocks)]

fn first_some() -> Option<()> {
    let _: Option<_> = try { return Some(foo()?) };
    let _: Option<_> = try { return Some(bar()?) };
    None
}

https://play.rust-lang.org/?gist=363157c898e0d067260e711db131b256&version=nightly&edition=2018

warning: unreachable expression
 --> src/main.rs:4:30
  |
4 |     let _: Option<_> = try { return Some(foo()?) };
  |                              ^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default

warning: unreachable expression
 --> src/main.rs:5:30
  |
5 |     let _: Option<_> = try { return Some(bar()?) };
  |                              ^^^^^^^^^^^^^^^^^^^

The desugar should probably just suppress this warning for the code it generates.

@zackmdavis zackmdavis added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Sep 13, 2018
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. F-try_blocks `#![feature(try_blocks)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
Centril added a commit to Centril/rust that referenced this issue Oct 1, 2019
…ode, r=Centril

Fix unreachable_code warnings for try{} block ok-wrapped expressions

Fixes rust-lang#54165 and fixes rust-lang#63324.
@bors bors closed this as completed in 18d0c03 Oct 2, 2019
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-try_blocks `#![feature(try_blocks)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants