-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect if-else chains with a missing final else in type errors
``` error[E0308]: `if` and `else` have incompatible types --> $DIR/if-else-chain-missing-else.rs:12:12 | LL | let x = if let Ok(x) = res { | ______________- LL | | x | | - expected because of this LL | | } else if let Err(e) = res { | | ____________^ LL | || return Err(e); LL | || }; | || ^ | ||_____| | |_____`if` and `else` have incompatible types | expected `i32`, found `()` | = note: `if` expressions without `else` evaluate to `()` = note: consider adding an `else` block that evaluates to the expected type ``` We probably want a longer explanation and fewer spans on this case. Partially address #133316.
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
enum Cause { Cause1, Cause2 } | ||
struct MyErr { x: Cause } | ||
|
||
fn main() { | ||
_ = f(); | ||
} | ||
|
||
fn f() -> Result<i32, MyErr> { | ||
let res = could_fail(); | ||
let x = if let Ok(x) = res { | ||
x | ||
} else if let Err(e) = res { //~ ERROR `if` and `else` | ||
return Err(e); | ||
}; | ||
Ok(x) | ||
} | ||
|
||
fn could_fail() -> Result<i32, MyErr> { | ||
Ok(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/if-else-chain-missing-else.rs:12:12 | ||
| | ||
LL | let x = if let Ok(x) = res { | ||
| ______________- | ||
LL | | x | ||
| | - expected because of this | ||
LL | | } else if let Err(e) = res { | ||
| | ____________^ | ||
LL | || return Err(e); | ||
LL | || }; | ||
| || ^ | ||
| ||_____| | ||
| |_____`if` and `else` have incompatible types | ||
| expected `i32`, found `()` | ||
| | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= note: consider adding an `else` block that evaluates to the expected type | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |