Skip to content

Commit c8d958d

Browse files
committed
Fix unreachable code warning for functions that return !
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.
1 parent 4f0b72e commit c8d958d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
409409
} else {
410410
quote! {
411411
if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> {
412+
#[allow(unreachable_code)]
412413
return __ret;
413414
}
414415
#(#decls)*

tests/test.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(
22
async_trait_nightly_testing,
3-
feature(impl_trait_in_assoc_type, min_specialization)
3+
feature(impl_trait_in_assoc_type, min_specialization, never_type)
44
)]
55
#![deny(rust_2021_compatibility, unused_qualifications)]
66
#![allow(
@@ -252,6 +252,25 @@ pub async fn test_unimplemented() {
252252
let _ = <() as Trait>::f;
253253
}
254254

255+
#[cfg(async_trait_nightly_testing)]
256+
pub async fn test_divering_function() {
257+
#[async_trait]
258+
pub trait Trait {
259+
async fn f() -> !;
260+
}
261+
262+
#[async_trait]
263+
impl Trait for () {
264+
async fn f() -> ! {
265+
loop {
266+
std::thread::sleep(std::time::Duration::from_millis(1));
267+
}
268+
}
269+
}
270+
271+
let _ = <() as Trait>::f;
272+
}
273+
255274
// https://github.com/dtolnay/async-trait/issues/1
256275
pub mod issue1 {
257276
use async_trait::async_trait;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy