forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#64581 - ztlpn:fix-ok-wrapping-unreachable-c…
…ode, r=Centril Fix unreachable_code warnings for try{} block ok-wrapped expressions Fixes rust-lang#54165 and fixes rust-lang#63324.
- Loading branch information
Showing
6 changed files
with
161 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Test unreachable_code lint for `try {}` block ok-wrapping. See issues #54165, #63324. | ||
|
||
// compile-flags: --edition 2018 | ||
// check-pass | ||
#![feature(try_blocks)] | ||
#![warn(unreachable_code)] | ||
|
||
fn err() -> Result<u32, ()> { | ||
Err(()) | ||
} | ||
|
||
// In the following cases unreachable code is autogenerated and should not be reported. | ||
|
||
fn test_ok_wrapped_divergent_expr_1() { | ||
let res: Result<u32, ()> = try { | ||
loop { | ||
err()?; | ||
} | ||
}; | ||
println!("res: {:?}", res); | ||
} | ||
|
||
fn test_ok_wrapped_divergent_expr_2() { | ||
let _: Result<u32, ()> = try { | ||
return | ||
}; | ||
} | ||
|
||
fn test_autogenerated_unit_after_divergent_expr() { | ||
let _: Result<(), ()> = try { | ||
return; | ||
}; | ||
} | ||
|
||
// In the following cases unreachable code should be reported. | ||
|
||
fn test_try_block_after_divergent_stmt() { | ||
let _: Result<u32, ()> = { | ||
return; | ||
|
||
try { | ||
loop { | ||
err()?; | ||
} | ||
} | ||
// ~^^^^^ WARNING unreachable expression | ||
}; | ||
} | ||
|
||
fn test_wrapped_divergent_expr() { | ||
let _: Result<u32, ()> = { | ||
Err(return) | ||
// ~^ WARNING unreachable call | ||
}; | ||
} | ||
|
||
fn test_expr_after_divergent_stmt_in_try_block() { | ||
let res: Result<u32, ()> = try { | ||
loop { | ||
err()?; | ||
} | ||
|
||
42 | ||
// ~^ WARNING unreachable expression | ||
}; | ||
println!("res: {:?}", res); | ||
} | ||
|
||
fn main() { | ||
test_ok_wrapped_divergent_expr_1(); | ||
test_ok_wrapped_divergent_expr_2(); | ||
test_autogenerated_unit_after_divergent_expr(); | ||
test_try_block_after_divergent_stmt(); | ||
test_wrapped_divergent_expr(); | ||
test_expr_after_divergent_stmt_in_try_block(); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/ui/try-block/try-block-unreachable-code-lint.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
warning: unreachable expression | ||
--> $DIR/try-block-unreachable-code-lint.rs:41:9 | ||
| | ||
LL | return; | ||
| ------ any code following this expression is unreachable | ||
LL | | ||
LL | / try { | ||
LL | | loop { | ||
LL | | err()?; | ||
LL | | } | ||
LL | | } | ||
| |_________^ unreachable expression | ||
| | ||
note: lint level defined here | ||
--> $DIR/try-block-unreachable-code-lint.rs:6:9 | ||
| | ||
LL | #![warn(unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
warning: unreachable call | ||
--> $DIR/try-block-unreachable-code-lint.rs:52:9 | ||
| | ||
LL | Err(return) | ||
| ^^^ ------ any code following this expression is unreachable | ||
| | | ||
| unreachable call | ||
|
||
warning: unreachable expression | ||
--> $DIR/try-block-unreachable-code-lint.rs:63:9 | ||
| | ||
LL | / loop { | ||
LL | | err()?; | ||
LL | | } | ||
| |_________- any code following this expression is unreachable | ||
LL | | ||
LL | 42 | ||
| ^^ unreachable expression | ||
|