Skip to content

Commit 369058e

Browse files
committed
Point at coercion reason for if exprs without else clause
``` error[E0317]: if may be missing an else clause --> $DIR/if-without-else-as-fn-expr.rs:2:5 | LL | fn foo(bar: usize) -> usize { | ----- found `usize` because of this return type LL | / if bar % 5 == 0 { LL | | return 3; LL | | } | |_____^ expected (), found usize | = note: expected type `()` found type `usize` = note: `if` expressions without `else` must evaluate to `()` ```
1 parent f22dca0 commit 369058e

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3472,8 +3472,39 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34723472
// We won't diverge unless both branches do (or the condition does).
34733473
self.diverges.set(cond_diverges | then_diverges & else_diverges);
34743474
} else {
3475+
// If this `if` expr is the parent's function return expr, the cause of the type
3476+
// coercion is the return type, point at it. (#25228)
3477+
let mut ret_reason = None;
3478+
if let Node::Block(block) = self.tcx.hir().get_by_hir_id(
3479+
self.tcx.hir().get_parent_node_by_hir_id(
3480+
self.tcx.hir().get_parent_node_by_hir_id(then_expr.hir_id),
3481+
),
3482+
) {
3483+
// check that the body's parent is an fn
3484+
let parent = self.tcx.hir().get_by_hir_id(
3485+
self.tcx.hir().get_parent_node_by_hir_id(
3486+
self.tcx.hir().get_parent_node_by_hir_id(block.hir_id),
3487+
),
3488+
);
3489+
if let (Some(expr), Node::Item(hir::Item {
3490+
node: hir::ItemKind::Fn(..), ..
3491+
})) = (&block.expr, parent) {
3492+
// check that the `if` expr without `else` is the fn body's expr
3493+
if expr.span == sp {
3494+
ret_reason = self.get_fn_decl(then_expr.hir_id).map(|(fn_decl, _)| (
3495+
fn_decl.output.span(),
3496+
format!("found `{}` because of this return type", fn_decl.output),
3497+
));
3498+
}
3499+
}
3500+
}
34753501
let else_cause = self.cause(sp, ObligationCauseCode::IfExpressionWithNoElse);
3476-
coerce.coerce_forced_unit(self, &else_cause, &mut |_| (), true);
3502+
coerce.coerce_forced_unit(self, &else_cause, &mut |err| {
3503+
if let Some((sp, msg)) = &ret_reason {
3504+
err.span_label(*sp, msg.as_str());
3505+
}
3506+
err.note("`if` expressions without `else` must evaluate to `()`");
3507+
}, true);
34773508

34783509
// If the condition is false we can't diverge.
34793510
self.diverges.set(cond_diverges);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn foo(bar: usize) -> usize {
2+
if bar % 5 == 0 {
3+
return 3;
4+
}
5+
//~^^^ ERROR if may be missing an else clause
6+
}
7+
8+
fn main() {
9+
let _ = foo(1);
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0317]: if may be missing an else clause
2+
--> $DIR/if-without-else-as-fn-expr.rs:2:5
3+
|
4+
LL | fn foo(bar: usize) -> usize {
5+
| ----- found `usize` because of this return type
6+
LL | / if bar % 5 == 0 {
7+
LL | | return 3;
8+
LL | | }
9+
| |_____^ expected (), found usize
10+
|
11+
= note: expected type `()`
12+
found type `usize`
13+
= note: `if` expressions without `else` must evaluate to `()`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0317`.

src/test/ui/if/if-without-else-result.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let a = if true { true };
66
|
77
= note: expected type `()`
88
found type `bool`
9+
= note: `if` expressions without `else` must evaluate to `()`
910

1011
error: aborting due to previous error
1112

src/test/ui/issues/issue-4201.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LL | | };
1313
|
1414
= note: expected type `()`
1515
found type `{integer}`
16+
= note: `if` expressions without `else` must evaluate to `()`
1617

1718
error: aborting due to previous error
1819

src/test/ui/issues/issue-50577.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | Drop = assert_eq!(1, 1)
66
|
77
= note: expected type `()`
88
found type `isize`
9+
= note: `if` expressions without `else` must evaluate to `()`
910
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1011

1112
error: aborting due to previous error

0 commit comments

Comments
 (0)