Add more details to elseless if error
This commit is contained in:
parent
ffa40cb45c
commit
dcaec88a57
6 changed files with 69 additions and 8 deletions
|
@ -3480,6 +3480,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
coerce.coerce_forced_unit(self, &else_cause, &mut |err| {
|
coerce.coerce_forced_unit(self, &else_cause, &mut |err| {
|
||||||
if let Some((sp, msg)) = &ret_reason {
|
if let Some((sp, msg)) = &ret_reason {
|
||||||
err.span_label(*sp, msg.as_str());
|
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.note("`if` expressions without `else` evaluate to `()`");
|
||||||
err.help("consider adding an `else` block that evaluates to the expected type");
|
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)> {
|
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(
|
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(
|
|
||||||
self.tcx.hir().get_parent_node_by_hir_id(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
|
// check that the body's parent is an fn
|
||||||
let parent = self.tcx.hir().get_by_hir_id(
|
let parent = 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(
|
||||||
|
@ -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
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,21 @@ fn foo(bar: usize) -> usize {
|
||||||
//~^^^ ERROR if may be missing an else clause
|
//~^^^ 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() {
|
fn main() {
|
||||||
let _ = foo(1);
|
let _ = foo(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,37 @@ LL | | }
|
||||||
= note: `if` expressions without `else` evaluate to `()`
|
= note: `if` expressions without `else` evaluate to `()`
|
||||||
= help: consider adding an `else` block that evaluates to the expected type
|
= 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`.
|
For more information about this error, try `rustc --explain E0317`.
|
||||||
|
|
|
@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause
|
||||||
--> $DIR/if-without-else-result.rs:2:13
|
--> $DIR/if-without-else-result.rs:2:13
|
||||||
|
|
|
|
||||||
LL | let a = if true { true };
|
LL | let a = if true { true };
|
||||||
| ^^^^^^^^^^^^^^^^ expected (), found bool
|
| ^^^^^^^^^^----^^
|
||||||
|
| | |
|
||||||
|
| | found here
|
||||||
|
| expected (), found bool
|
||||||
|
|
|
|
||||||
= note: expected type `()`
|
= note: expected type `()`
|
||||||
found type `bool`
|
found type `bool`
|
||||||
|
|
|
@ -8,6 +8,7 @@ LL | | //~| expected type `()`
|
||||||
LL | | //~| found type `{integer}`
|
LL | | //~| found type `{integer}`
|
||||||
LL | | //~| expected (), found integer
|
LL | | //~| expected (), found integer
|
||||||
LL | | 1
|
LL | | 1
|
||||||
|
| | - found here
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^ expected (), found integer
|
| |_____^ expected (), found integer
|
||||||
|
|
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ error[E0317]: if may be missing an else clause
|
||||||
--> $DIR/issue-50577.rs:3:16
|
--> $DIR/issue-50577.rs:3:16
|
||||||
|
|
|
|
||||||
LL | Drop = assert_eq!(1, 1)
|
LL | Drop = assert_eq!(1, 1)
|
||||||
| ^^^^^^^^^^^^^^^^ expected (), found isize
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
| |
|
||||||
|
| expected (), found isize
|
||||||
|
| found here
|
||||||
|
|
|
|
||||||
= note: expected type `()`
|
= note: expected type `()`
|
||||||
found type `isize`
|
found type `isize`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue