Skip to content

Conversation

ry-sun
Copy link
Contributor

@ry-sun ry-sun commented Sep 29, 2025

This PR intent to resolve bug mentioned in #7650

Motivation

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

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")
}

After modification:

https://github.com/ry-sun/tokio/blob/2b9524813e01f61d3f108af25b61f8a661af59c8/tokio-macros/src/entry.rs#L467-L483

@ADD-SP ADD-SP added the A-tokio-macros Area: The tokio-macros crate label Sep 29, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update your branch, we fixed the CI failures recently.

@ry-sun
Copy link
Contributor Author

ry-sun commented Oct 1, 2025

@ADD-SP Sure, I have rebased my branch to master~

@ADD-SP ADD-SP changed the title fix: suppress intentional clippy rule clippy::unwrap_in_result (fix #7650) macros: suppress clippy::unwrap_in_result in #[tokio::main] Oct 2, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ry-sun
Copy link
Contributor Author

ry-sun commented Oct 2, 2025

@ADD-SP I updated again to fit bash branch, and seems it can be cleanly merged right now. 😄

@ADD-SP ADD-SP merged commit c1f0c76 into tokio-rs:master Oct 2, 2025
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-macros Area: The tokio-macros crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants