Closed
Description
rustc version: rustc 1.65.0-nightly (7480389 2022-08-25)
I have a very small reproducer for a weird fix advice from rustc when the two branches of an if had a type mismatch:
fn main() {
let x = if true {
Ok(Ok(()))
} else {
Ok(())
};
}
This will produce the following output:
error[E0308]: `if` and `else` have incompatible types
--> weird-fix-advice-else-ok.rs:5:9
|
2 | let x = if true {
| _____________-
3 | | Ok(Ok(()))
| | ---------- expected because of this
4 | | } else {
5 | | Ok(())
| | ^^^^^^ expected enum `Result`, found `()`
6 | | };
| |_____- `if` and `else` have incompatible types
|
= note: expected enum `Result<Result<(), _>, _>`
found enum `Result<(), _>`
help: try wrapping the expression in a variant of `Result`
|
4 ~ } else Ok({
5 | Ok(())
6 ~ });
|
4 ~ } else Err({
5 | Ok(())
6 ~ });
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Applying this fix obviously doesn't help:
error: expected `{`, found `Ok`
--> weird-fix-advice-else-ok.rs:4:12
|
4 | } else Ok({
| ^^ expected `{`
|
help: try placing this code inside a block
|
4 ~ } else { Ok({
5 | Ok(())
6 ~ }) };
|
error: aborting due to previous error