Skip to content

Commit

Permalink
Fix unreachable code warning for functions that return !
Browse files Browse the repository at this point in the history
The generated code for function bodies produces an `unreachable_code`
warning if the function has a `!` return type.

The offending bit is:
```rust
if let Some(__ret) == None::<!> {
  // unreachable
}
```

This PR fixes the warning by simply adding `#[allow(unreachable_code)]`
to the return statement. It also adds a test for it.

This problem can only be triggered on nightly with the `never_type`
feature enabled, but it is easy to fix here.
  • Loading branch information
de-vri-es committed Apr 11, 2024
1 parent 4f0b72e commit c8d958d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
} else {
quote! {
if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> {
#[allow(unreachable_code)]
return __ret;
}
#(#decls)*
Expand Down
21 changes: 20 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg_attr(
async_trait_nightly_testing,
feature(impl_trait_in_assoc_type, min_specialization)
feature(impl_trait_in_assoc_type, min_specialization, never_type)
)]
#![deny(rust_2021_compatibility, unused_qualifications)]
#![allow(
Expand Down Expand Up @@ -252,6 +252,25 @@ pub async fn test_unimplemented() {
let _ = <() as Trait>::f;
}

#[cfg(async_trait_nightly_testing)]
pub async fn test_divering_function() {
#[async_trait]
pub trait Trait {
async fn f() -> !;
}

#[async_trait]
impl Trait for () {
async fn f() -> ! {
loop {
std::thread::sleep(std::time::Duration::from_millis(1));
}
}
}

let _ = <() as Trait>::f;
}

// https://github.com/dtolnay/async-trait/issues/1
pub mod issue1 {
use async_trait::async_trait;
Expand Down

0 comments on commit c8d958d

Please sign in to comment.