Rollup merge of #69287 - GuillaumeGomez:clean-e0317, r=Dylan-DPC

Clean up E0317 explanation

r? @Dylan-DPC
This commit is contained in:
Dylan DPC 2020-02-20 10:49:14 +01:00 committed by GitHub
commit 941ce1a557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 7 deletions

View File

@ -1,14 +1,30 @@
This error occurs when an `if` expression without an `else` block is used in a
context where a type other than `()` is expected, for example a `let`
expression:
An `if` expression is missing an `else` block.
Erroneous code example:
```compile_fail,E0317
fn main() {
let x = 5;
let a = if x == 5 { 1 };
}
let x = 5;
let a = if x == 5 {
1
};
```
This error occurs when an `if` expression without an `else` block is used in a
context where a type other than `()` is expected. In the previous code example,
the `let` expression was expecting a value but since there was no `else`, no
value was returned.
An `if` expression without an `else` block has the type `()`, so this is a type
error. To resolve it, add an `else` block having the same type as the `if`
block.
So to fix the previous code example:
```
let x = 5;
let a = if x == 5 {
1
} else {
2
};
```