Auto merge of #58981 - estebank:elseless-if, r=davidtwco

Point at coercion reason for `if` expressions without else clause if caused by return type

```
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 `()`
```

Fix #25228.
This commit is contained in:
bors 2019-03-21 23:42:41 +00:00
commit 86466a397a
6 changed files with 135 additions and 3 deletions

View file

@ -0,0 +1,25 @@
fn foo(bar: usize) -> usize {
if bar % 5 == 0 {
return 3;
}
//~^^^ 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

@ -0,0 +1,49 @@
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 {
| ----- expected `usize` because of this return type
LL | / if bar % 5 == 0 {
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: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,10 +2,15 @@ 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`
= 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

View file

@ -8,11 +8,14 @@ LL | |
LL | |
LL | |
LL | | 1
| | - found here
LL | | };
| |_____^ expected (), found integer
|
= note: expected type `()`
found type `{integer}`
= 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

View file

@ -2,10 +2,15 @@ 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`
= note: `if` expressions without `else` evaluate to `()`
= help: consider adding an `else` block that evaluates to the expected type
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to previous error