Add more details to elseless if error

This commit is contained in:
Esteban Küber 2019-03-07 14:37:18 -08:00
parent ffa40cb45c
commit dcaec88a57
6 changed files with 69 additions and 8 deletions

View file

@ -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);
}

View file

@ -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`.

View file

@ -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`

View file

@ -8,6 +8,7 @@ LL | | //~| expected type `()`
LL | | //~| found type `{integer}`
LL | | //~| expected (), found integer
LL | | 1
| | - found here
LL | | };
| |_____^ expected (), found integer
|

View file

@ -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`