Skip to content

Bug: #[tokio::main] triggers clippy::unwrap_in_result via internal expect #7650

@ry-sun

Description

@ry-sun

Version
List the versions of all tokio crates you are using. The easiest way to get
this information is using cargo tree subcommand:

cargo tree | grep tokio

newest

├── tokio v1.47.1
│   └── tokio-macros v2.5.0 (proc-macro)

Platform
The output of uname -a (UNIX), or version and 32 or 64-bit (Windows)

Darwin RySun-Macbook-Air-M2 25.0.0 Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:53 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T8112 arm64 arm Darwin

Description

When user explicitly set on clippy lint rule, clippy::unwrap_in_result, and use macro tokyo::main, they will get error.

An example test program:

# rust-toolchain.toml
[toolchain]
channel = "nightly"
# Cargo.toml
[package]
name = "tokio-test"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.100"
tokio = { version = "1.47.1", features = ["macros", "rt", "rt-multi-thread"] }

[lints.clippy]
unwrap_in_result = "deny
// main.rs
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    println!("Hello, world!");

    Ok(())
}

Than, run command cargo clippy:

# Output of command `cargo clippy`
error: `expect` used in a function that returns a `Result`
 --> src/main.rs:7:5
  |
7 |     Ok(())
  |     ^^^^^^
  |
note: in this function signature
 --> src/main.rs:4:20
  |
4 | async fn main() -> Result<()> {
  |                    ^^^^^^^^^^
  = help: consider using the `?` operator or calling the `.map_err()` method
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_in_result
  = note: requested on the command line with `-D clippy::unwrap-in-result`

error: could not compile `tokio-test` (bin "tokio-test") due to 1 previous error

Possible Solution

I suggest tokio to suppress this clippy rule (clippy::unwrap_in_result) in macro expansion, just like suppressing other rules in this file

let last_block = quote_spanned! {last_stmt_end_span=>
#do_checks
#[cfg(all(#(#checks),*))]
#[allow(clippy::expect_used, clippy::diverging_sub_expression, clippy::needless_return)]
{
return #rt
.enable_all()
.#build
.expect("Failed building the Runtime")
.block_on(#body_ident);
}
#[cfg(not(all(#(#checks),*)))]
{
panic!("fell through checks")
}

I already have opened a PR to resolve this bug by the solution I suggest. If this solution help, please consider merge it. 😄 Thank you~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions