Skip to content

Commit dcaec88

Browse files
committed
Add more details to elseless if error
1 parent ffa40cb commit dcaec88

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

Diff for: src/librustc_typeck/check/mod.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34803480
coerce.coerce_forced_unit(self, &else_cause, &mut |err| {
34813481
if let Some((sp, msg)) = &ret_reason {
34823482
err.span_label(*sp, msg.as_str());
3483+
} else if let ExprKind::Block(block, _) = &then_expr.node {
3484+
if let Some(expr) = &block.expr {
3485+
err.span_label(expr.span, "found here".to_string());
3486+
}
34833487
}
34843488
err.note("`if` expressions without `else` evaluate to `()`");
34853489
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> {
34983502
}
34993503

35003504
fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, sp: Span) -> Option<(Span, String)> {
3501-
if let Node::Block(block) = self.tcx.hir().get_by_hir_id(
3502-
self.tcx.hir().get_parent_node_by_hir_id(
3503-
self.tcx.hir().get_parent_node_by_hir_id(hir_id),
3504-
),
3505-
) {
3505+
let node = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(
3506+
self.tcx.hir().get_parent_node_by_hir_id(hir_id),
3507+
));
3508+
if let Node::Block(block) = node {
35063509
// check that the body's parent is an fn
35073510
let parent = self.tcx.hir().get_by_hir_id(
35083511
self.tcx.hir().get_parent_node_by_hir_id(
@@ -3521,6 +3524,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
35213524
}
35223525
}
35233526
}
3527+
if let Node::Local(hir::Local {
3528+
ty: Some(_), pat, ..
3529+
}) = node {
3530+
return Some((pat.span, "expected because of this assignment".to_string()));
3531+
}
35243532
None
35253533
}
35263534

Diff for: src/test/ui/if/if-without-else-as-fn-expr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ fn foo(bar: usize) -> usize {
55
//~^^^ ERROR if may be missing an else clause
66
}
77

8+
fn foo2(bar: usize) -> usize {
9+
let x: usize = if bar % 5 == 0 {
10+
return 3;
11+
};
12+
//~^^^ ERROR if may be missing an else clause
13+
x
14+
}
15+
16+
fn foo3(bar: usize) -> usize {
17+
if bar % 5 == 0 {
18+
3
19+
}
20+
//~^^^ ERROR if may be missing an else clause
21+
}
22+
823
fn main() {
924
let _ = foo(1);
1025
}

Diff for: src/test/ui/if/if-without-else-as-fn-expr.stderr

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@ LL | | }
1313
= note: `if` expressions without `else` evaluate to `()`
1414
= help: consider adding an `else` block that evaluates to the expected type
1515

16-
error: aborting due to previous error
16+
error[E0317]: if may be missing an else clause
17+
--> $DIR/if-without-else-as-fn-expr.rs:9:20
18+
|
19+
LL | let x: usize = if bar % 5 == 0 {
20+
| _________-__________^
21+
| | |
22+
| | expected because of this assignment
23+
LL | | return 3;
24+
LL | | };
25+
| |_____^ expected usize, found ()
26+
|
27+
= note: expected type `usize`
28+
found type `()`
29+
= note: `if` expressions without `else` evaluate to `()`
30+
= help: consider adding an `else` block that evaluates to the expected type
31+
32+
error[E0317]: if may be missing an else clause
33+
--> $DIR/if-without-else-as-fn-expr.rs:17:5
34+
|
35+
LL | fn foo3(bar: usize) -> usize {
36+
| ----- expected `usize` because of this return type
37+
LL | / if bar % 5 == 0 {
38+
LL | | 3
39+
LL | | }
40+
| |_____^ expected usize, found ()
41+
|
42+
= note: expected type `usize`
43+
found type `()`
44+
= note: `if` expressions without `else` evaluate to `()`
45+
= help: consider adding an `else` block that evaluates to the expected type
46+
47+
error: aborting due to 3 previous errors
1748

1849
For more information about this error, try `rustc --explain E0317`.

Diff for: src/test/ui/if/if-without-else-result.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause
22
--> $DIR/if-without-else-result.rs:2:13
33
|
44
LL | let a = if true { true };
5-
| ^^^^^^^^^^^^^^^^ expected (), found bool
5+
| ^^^^^^^^^^----^^
6+
| | |
7+
| | found here
8+
| expected (), found bool
69
|
710
= note: expected type `()`
811
found type `bool`

Diff for: src/test/ui/issues/issue-4201.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | | //~| expected type `()`
88
LL | | //~| found type `{integer}`
99
LL | | //~| expected (), found integer
1010
LL | | 1
11+
| | - found here
1112
LL | | };
1213
| |_____^ expected (), found integer
1314
|

Diff for: src/test/ui/issues/issue-50577.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause
22
--> $DIR/issue-50577.rs:3:16
33
|
44
LL | Drop = assert_eq!(1, 1)
5-
| ^^^^^^^^^^^^^^^^ expected (), found isize
5+
| ^^^^^^^^^^^^^^^^
6+
| |
7+
| expected (), found isize
8+
| found here
69
|
710
= note: expected type `()`
811
found type `isize`

0 commit comments

Comments
 (0)