diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c7aea137b6841..f4ecc0973ef29 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 0000000000000..119ffccc6a945 --- /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 0000000000000..2631f1e426ded --- /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`.