Open
Description
Code
#[tokio::main]
async fn main() {
if true {
return Ok(());
}
let something_else = 1;
}
Current output
error[E0308]: mismatched types
--> src/main.rs:2:17
|
2 | async fn main() {
| _________________^
3 | | if true {
4 | | return Ok(());
5 | | }
6 | | let something_else = 1;
7 | | }
| |_^ expected `Result<(), _>`, found `()`
|
= note: expected enum `Result<(), _>`
found unit type `()`
error[E0308]: mismatched types
--> src/main.rs:3:5
|
2 | async fn main() {
| - expected `()` because of default return type
3 | / if true {
4 | | return Ok(());
5 | | }
6 | | let something_else = 1;
| |___________________________^ expected `()`, found `Result<(), _>`
|
= note: expected unit type `()`
found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
|
6 | let something_else = 1;.expect("REASON")
| +++++++++++++++++
Desired output
error[E0308]: mismatched types
--> src/main.rs:3:5
|
2 | async fn main() {
| - expected `()` because of default return type
3 | if true {
4 | return Ok(());
| ^^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
|
= note: expected unit type `()`
found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
|
4 | return Ok(()).expect("REASON");
| +++++++++++++++++
Rationale and extra context
This is probably just caused by the #[tokio::main]
proc-macro. It's confusing that absolutely everything after (and inside of) the bad return statement scope is seen as a bad statement, and that the help
section suggests adding .expect("REASON")
after a semicolon at the very end.
Found this because copy-pasted code contained return Ok(());
, which caused the scope and everything after it to be highlighted as an error with no further explanation.
Other cases
Rust Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
Anything else?
Return statements without a scope and at the end get highlighted semi-correctly:
#[tokio::main]
async fn main() {
return Ok(());
}
Produces
error[E0308]: mismatched types
--> src/main.rs:3:5
|
2 | async fn main() {
| - expected `()` because of default return type
3 | return Ok(());
| ^^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
|
= note: expected unit type `()`
found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
|
3 | return Ok(());.expect("REASON")
| +++++++++++++++++
The error span is fine, but the help
isn't