summaryrefslogtreecommitdiffstats
path: root/src/test/ui/try-block/try-block-unreachable-code-lint.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/try-block/try-block-unreachable-code-lint.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/try-block/try-block-unreachable-code-lint.rs')
-rw-r--r--src/test/ui/try-block/try-block-unreachable-code-lint.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/ui/try-block/try-block-unreachable-code-lint.rs b/src/test/ui/try-block/try-block-unreachable-code-lint.rs
new file mode 100644
index 000000000..e1d82ea36
--- /dev/null
+++ b/src/test/ui/try-block/try-block-unreachable-code-lint.rs
@@ -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();
+}