Closed
Description
Given the following code:
fn missing_else(x: i32) -> i32 {
if true {
return 1
}
}
fn missing_else_nested(x: i32) -> i32 {
if x < 0 {
0
} else {
if true {
return 1
}
}
}
The current output is:
error[E0317]: `if` may be missing an `else` clause
--> src/lib.rs:2:5
|
1 | fn missing_else(x: i32) -> i32 {
| --- expected `i32` because of this return type
2 | / if true {
3 | | return 1
4 | | }
| |_____^ expected `i32`, found `()`
|
= 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
--> src/lib.rs:11:9
|
11 | / if true {
12 | | return 1
| | -------- found here
13 | | }
| |_________^ expected `()`, found `i32`
|
= note: `if` expressions without `else` evaluate to `()`
= help: consider adding an `else` block that evaluates to the expected type
Note how the expected and found types are reversed for the missing_else_nested
error and how the note about the function's return type is gone, compared to the missing_else
error.
Ideally the output for the missing_else_nested
error should be similar to the error for missing_else
:
error[E0317]: `if` may be missing an `else` clause
--> src/lib.rs:11:9
|
7 | fn missing_else_nested(x: i32) -> i32 {
| --- expected `i32` because of this return type
|
11 | / if true {
12 | | return 1
| | -------- found here
13 | | }
| |_________^ expected `i32`, found `()`
|
= note: `if` expressions without `else` evaluate to `()`
= help: consider adding an `else` block that evaluates to the expected type
This kinda duplicates #78656, but @estebank asked me to file a new issue on Twitter.