diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 703a0dae633..f2ab78e16fb 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3480,6 +3480,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { coerce.coerce_forced_unit(self, &else_cause, &mut |err| { if let Some((sp, msg)) = &ret_reason { err.span_label(*sp, msg.as_str()); + } else if let ExprKind::Block(block, _) = &then_expr.node { + if let Some(expr) = &block.expr { + err.span_label(expr.span, "found here".to_string()); + } } err.note("`if` expressions without `else` evaluate to `()`"); err.help("consider adding an `else` block that evaluates to the expected type"); @@ -3498,11 +3502,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, sp: Span) -> Option<(Span, String)> { - if let Node::Block(block) = self.tcx.hir().get_by_hir_id( - self.tcx.hir().get_parent_node_by_hir_id( - self.tcx.hir().get_parent_node_by_hir_id(hir_id), - ), - ) { + let node = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id( + self.tcx.hir().get_parent_node_by_hir_id(hir_id), + )); + if let Node::Block(block) = node { // check that the body's parent is an fn let parent = self.tcx.hir().get_by_hir_id( self.tcx.hir().get_parent_node_by_hir_id( @@ -3521,6 +3524,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } } + if let Node::Local(hir::Local { + ty: Some(_), pat, .. + }) = node { + return Some((pat.span, "expected because of this assignment".to_string())); + } None } diff --git a/src/test/ui/if/if-without-else-as-fn-expr.rs b/src/test/ui/if/if-without-else-as-fn-expr.rs index 76ffb49697f..67e4445629f 100644 --- a/src/test/ui/if/if-without-else-as-fn-expr.rs +++ b/src/test/ui/if/if-without-else-as-fn-expr.rs @@ -5,6 +5,21 @@ fn foo(bar: usize) -> usize { //~^^^ ERROR if may be missing an else clause } +fn foo2(bar: usize) -> usize { + let x: usize = if bar % 5 == 0 { + return 3; + }; + //~^^^ ERROR if may be missing an else clause + x +} + +fn foo3(bar: usize) -> usize { + if bar % 5 == 0 { + 3 + } + //~^^^ ERROR if may be missing an else clause +} + fn main() { let _ = foo(1); } diff --git a/src/test/ui/if/if-without-else-as-fn-expr.stderr b/src/test/ui/if/if-without-else-as-fn-expr.stderr index 062e0b9c44d..0ba72726ca7 100644 --- a/src/test/ui/if/if-without-else-as-fn-expr.stderr +++ b/src/test/ui/if/if-without-else-as-fn-expr.stderr @@ -13,6 +13,37 @@ LL | | } = note: `if` expressions without `else` evaluate to `()` = help: consider adding an `else` block that evaluates to the expected type -error: aborting due to previous error +error[E0317]: if may be missing an else clause + --> $DIR/if-without-else-as-fn-expr.rs:9:20 + | +LL | let x: usize = if bar % 5 == 0 { + | _________-__________^ + | | | + | | expected because of this assignment +LL | | return 3; +LL | | }; + | |_____^ expected usize, found () + | + = note: expected type `usize` + found type `()` + = note: `if` expressions without `else` evaluate to `()` + = help: consider adding an `else` block that evaluates to the expected type + +error[E0317]: if may be missing an else clause + --> $DIR/if-without-else-as-fn-expr.rs:17:5 + | +LL | fn foo3(bar: usize) -> usize { + | ----- expected `usize` because of this return type +LL | / if bar % 5 == 0 { +LL | | 3 +LL | | } + | |_____^ expected usize, found () + | + = note: expected type `usize` + found type `()` + = note: `if` expressions without `else` evaluate to `()` + = help: consider adding an `else` block that evaluates to the expected type + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0317`. diff --git a/src/test/ui/if/if-without-else-result.stderr b/src/test/ui/if/if-without-else-result.stderr index cb1df141bcb..ddb013ab711 100644 --- a/src/test/ui/if/if-without-else-result.stderr +++ b/src/test/ui/if/if-without-else-result.stderr @@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause --> $DIR/if-without-else-result.rs:2:13 | LL | let a = if true { true }; - | ^^^^^^^^^^^^^^^^ expected (), found bool + | ^^^^^^^^^^----^^ + | | | + | | found here + | expected (), found bool | = note: expected type `()` found type `bool` diff --git a/src/test/ui/issues/issue-4201.stderr b/src/test/ui/issues/issue-4201.stderr index 53397c8ec90..4d7116a0ee9 100644 --- a/src/test/ui/issues/issue-4201.stderr +++ b/src/test/ui/issues/issue-4201.stderr @@ -8,6 +8,7 @@ LL | | //~| expected type `()` LL | | //~| found type `{integer}` LL | | //~| expected (), found integer LL | | 1 + | | - found here LL | | }; | |_____^ expected (), found integer | diff --git a/src/test/ui/issues/issue-50577.stderr b/src/test/ui/issues/issue-50577.stderr index 323f5ac6547..0c3ba2ea4f9 100644 --- a/src/test/ui/issues/issue-50577.stderr +++ b/src/test/ui/issues/issue-50577.stderr @@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause --> $DIR/issue-50577.rs:3:16 | LL | Drop = assert_eq!(1, 1) - | ^^^^^^^^^^^^^^^^ expected (), found isize + | ^^^^^^^^^^^^^^^^ + | | + | expected (), found isize + | found here | = note: expected type `()` found type `isize`