don't warn on explicit casts of never to any

This commit is contained in:
Waffle Lapkin 2025-06-01 12:51:17 +02:00
parent a067c6a3c6
commit d2e133d96a
No known key found for this signature in database
5 changed files with 20 additions and 35 deletions

View File

@ -290,6 +290,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
| ExprKind::Let(..)
| ExprKind::Loop(..)
| ExprKind::Match(..) => {}
// Do not warn on `as` casts from never to any,
// they are sometimes required to appeal typeck.
ExprKind::Cast(_, _) => {}
// If `expr` is a result of desugaring the try block and is an ok-wrapped
// diverging expression (e.g. it arose from desugaring of `try { return }`),
// we skip issuing a warning because it is autogenerated code.

View File

@ -1,12 +1,21 @@
//@ check-pass
//@ edition: 2024
//
// Check that we don't warn on `as` casts of never to any as unreachable.
// While they *are* unreachable, sometimes they are required to appeal typeck.
#![deny(unreachable_code)]
fn a() {
_ = {return} as u32; //~ error: unreachable
_ = {return} as u32;
}
fn b() {
(return) as u32; //~ error: unreachable
(return) as u32;
}
// example that needs an explicit never-to-any `as` cast
fn example() -> impl Iterator<Item = u8> {
todo!() as std::iter::Empty<_>
}
fn main() {}

View File

@ -1,26 +0,0 @@
error: unreachable expression
--> $DIR/expr_cast.rs:5:9
|
LL | _ = {return} as u32;
| ^------^^^^^^^^
| ||
| |any code following this expression is unreachable
| unreachable expression
|
note: the lint level is defined here
--> $DIR/expr_cast.rs:2:9
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
error: unreachable expression
--> $DIR/expr_cast.rs:9:5
|
LL | (return) as u32;
| --------^^^^^^^
| |
| unreachable expression
| any code following this expression is unreachable
error: aborting due to 2 previous errors

View File

@ -18,7 +18,7 @@ fn bar(x: Result<!, i32>) -> Result<u32, i32> {
fn foo(x: Result<!, i32>) -> Result<u32, i32> {
let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
//~^ WARN unreachable pattern
//~| WARN unreachable expression
//~| WARN unreachable call
Ok(y)
}

View File

@ -1,11 +1,10 @@
warning: unreachable expression
--> $DIR/unreachable-try-pattern.rs:19:36
warning: unreachable call
--> $DIR/unreachable-try-pattern.rs:19:33
|
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
| -^^^^^^^
| |
| unreachable expression
| any code following this expression is unreachable
| ^^ - any code following this expression is unreachable
| |
| unreachable call
|
note: the lint level is defined here
--> $DIR/unreachable-try-pattern.rs:3:9