Skip to content

Commit

Permalink
Rollup merge of #69287 - GuillaumeGomez:clean-e0317, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Clean up E0317 explanation

r? @Dylan-DPC
  • Loading branch information
Dylan-DPC authored Feb 20, 2020
2 parents 5d285dc + 1b342f7 commit 941ce1a
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/librustc_error_codes/error_codes/E0317.md
Original file line number Diff line number Diff line change
@@ -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
};
```

0 comments on commit 941ce1a

Please sign in to comment.