Skip to content

Commit 941ce1a

Browse files
authored
Rollup merge of #69287 - GuillaumeGomez:clean-e0317, r=Dylan-DPC
Clean up E0317 explanation r? @Dylan-DPC
2 parents 5d285dc + 1b342f7 commit 941ce1a

File tree

1 file changed

+23
-7
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+23
-7
lines changed
+23-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
This error occurs when an `if` expression without an `else` block is used in a
2-
context where a type other than `()` is expected, for example a `let`
3-
expression:
1+
An `if` expression is missing an `else` block.
2+
3+
Erroneous code example:
44

55
```compile_fail,E0317
6-
fn main() {
7-
let x = 5;
8-
let a = if x == 5 { 1 };
9-
}
6+
let x = 5;
7+
let a = if x == 5 {
8+
1
9+
};
1010
```
1111

12+
This error occurs when an `if` expression without an `else` block is used in a
13+
context where a type other than `()` is expected. In the previous code example,
14+
the `let` expression was expecting a value but since there was no `else`, no
15+
value was returned.
16+
1217
An `if` expression without an `else` block has the type `()`, so this is a type
1318
error. To resolve it, add an `else` block having the same type as the `if`
1419
block.
20+
21+
So to fix the previous code example:
22+
23+
```
24+
let x = 5;
25+
let a = if x == 5 {
26+
1
27+
} else {
28+
2
29+
};
30+
```

0 commit comments

Comments
 (0)