mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 02:33:55 +00:00
Add more details to elseless if error
This commit is contained in:
parent
ffa40cb45c
commit
dcaec88a57
@ -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
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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`.
|
||||
|
@ -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`
|
||||
|
@ -8,6 +8,7 @@ LL | | //~| expected type `()`
|
||||
LL | | //~| found type `{integer}`
|
||||
LL | | //~| expected (), found integer
|
||||
LL | | 1
|
||||
| | - found here
|
||||
LL | | };
|
||||
| |_____^ expected (), found integer
|
||||
|
|
||||
|
@ -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`
|
||||
|
Loading…
Reference in New Issue
Block a user