about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-05-20 18:13:47 +0200
committerGitHub <noreply@github.com>2024-05-20 18:13:47 +0200
commitba1bb80b6b1cd17c4114e8e25f3a8f650de93810 (patch)
tree7dd3e465cbd411f4ae37ffc116c79934f21d77d7
parent29c603c1fa66504fd05d26b4cb33821c87bd9479 (diff)
parentc811acb1f36cdeb45b46c490b7a308851347dc0d (diff)
downloadrust-ba1bb80b6b1cd17c4114e8e25f3a8f650de93810.tar.gz
rust-ba1bb80b6b1cd17c4114e8e25f3a8f650de93810.zip
Rollup merge of #124917 - cardigan1008:issue-124819, r=pnkfelix
Check whether the next_node is else-less if in get_return_block

Fix #124819
-rw-r--r--compiler/rustc_middle/src/hir/map/mod.rs1
-rw-r--r--tests/ui/return/tail-expr-if-as-return.rs5
-rw-r--r--tests/ui/return/tail-expr-if-as-return.stderr12
3 files changed, 18 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs
index c7aea137b68..f4ecc0973ef 100644
--- a/compiler/rustc_middle/src/hir/map/mod.rs
+++ b/compiler/rustc_middle/src/hir/map/mod.rs
@@ -549,6 +549,7 @@ impl<'hir> Map<'hir> {
                     Node::Block(Block { expr: None, .. }) => return None,
                     // The current node is not the tail expression of its parent.
                     Node::Block(Block { expr: Some(e), .. }) if hir_id != e.hir_id => return None,
+                    Node::Block(Block { expr: Some(e), ..}) if matches!(e.kind, ExprKind::If(_, _, None)) => return None,
                     _ => {}
                 }
             }
diff --git a/tests/ui/return/tail-expr-if-as-return.rs b/tests/ui/return/tail-expr-if-as-return.rs
new file mode 100644
index 00000000000..119ffccc6a9
--- /dev/null
+++ b/tests/ui/return/tail-expr-if-as-return.rs
@@ -0,0 +1,5 @@
+fn main() {
+    if true {
+        "" //~ ERROR mismatched types [E0308]
+    }
+}
diff --git a/tests/ui/return/tail-expr-if-as-return.stderr b/tests/ui/return/tail-expr-if-as-return.stderr
new file mode 100644
index 00000000000..2631f1e426d
--- /dev/null
+++ b/tests/ui/return/tail-expr-if-as-return.stderr
@@ -0,0 +1,12 @@
+error[E0308]: mismatched types
+  --> $DIR/tail-expr-if-as-return.rs:3:9
+   |
+LL | /     if true {
+LL | |         ""
+   | |         ^^ expected `()`, found `&str`
+LL | |     }
+   | |_____- expected this to be `()`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0308`.