Skip to content

Commit d2e133d

Browse files
committed
don't warn on explicit casts of never to any
1 parent a067c6a commit d2e133d

File tree

5 files changed

+20
-35
lines changed

5 files changed

+20
-35
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
290290
| ExprKind::Let(..)
291291
| ExprKind::Loop(..)
292292
| ExprKind::Match(..) => {}
293+
// Do not warn on `as` casts from never to any,
294+
// they are sometimes required to appeal typeck.
295+
ExprKind::Cast(_, _) => {}
293296
// If `expr` is a result of desugaring the try block and is an ok-wrapped
294297
// diverging expression (e.g. it arose from desugaring of `try { return }`),
295298
// we skip issuing a warning because it is autogenerated code.

tests/ui/reachable/expr_cast.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
//@ check-pass
12
//@ edition: 2024
3+
//
4+
// Check that we don't warn on `as` casts of never to any as unreachable.
5+
// While they *are* unreachable, sometimes they are required to appeal typeck.
26
#![deny(unreachable_code)]
37

48
fn a() {
5-
_ = {return} as u32; //~ error: unreachable
9+
_ = {return} as u32;
610
}
711

812
fn b() {
9-
(return) as u32; //~ error: unreachable
13+
(return) as u32;
14+
}
15+
16+
// example that needs an explicit never-to-any `as` cast
17+
fn example() -> impl Iterator<Item = u8> {
18+
todo!() as std::iter::Empty<_>
1019
}
1120

1221
fn main() {}

tests/ui/reachable/expr_cast.stderr

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/ui/reachable/unreachable-try-pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn bar(x: Result<!, i32>) -> Result<u32, i32> {
1818
fn foo(x: Result<!, i32>) -> Result<u32, i32> {
1919
let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
2020
//~^ WARN unreachable pattern
21-
//~| WARN unreachable expression
21+
//~| WARN unreachable call
2222
Ok(y)
2323
}
2424

tests/ui/reachable/unreachable-try-pattern.stderr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
warning: unreachable expression
2-
--> $DIR/unreachable-try-pattern.rs:19:36
1+
warning: unreachable call
2+
--> $DIR/unreachable-try-pattern.rs:19:33
33
|
44
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
5-
| -^^^^^^^
6-
| |
7-
| unreachable expression
8-
| any code following this expression is unreachable
5+
| ^^ - any code following this expression is unreachable
6+
| |
7+
| unreachable call
98
|
109
note: the lint level is defined here
1110
--> $DIR/unreachable-try-pattern.rs:3:9

0 commit comments

Comments
 (0)