Repository: tokio-rs/tokio (language: rust)

Issue:
Clippy warns "unneeded `return` statement" for `#[tokio::{main,test}]` fns on latest nightly
**Version**

tokio 1.40.0
rust toolchain 1.83.0-nightly (9e394f551 2024-09-25)

**Platform**

`Linux invar 6.10.10 #1-NixOS SMP PREEMPT_DYNAMIC Thu Sep 12 09:13:13 UTC 2024 x86_64 GNU/Linux`

**Description**

I tried this code:

```rust
// lib.rs
#[tokio::main(flavor = "current_thread")]
async fn main() {
    println!("hello");
}
```

with

```toml
# Cargo.toml
[package]
name = "poc"
edition = "2021"
version = "0.0.0"

[dependencies]
tokio = { version = "1.40.0", features = ["rt", "macros"] }
```

When running `cargo clippy --tests -- -Dclippy::needless_return`

I expected to see this happen: [no warnings]

Instead, this happened:
```
    Checking poc v0.0.0 (/home/oxa/tmp/rust-poc)
error: unneeded `return` statement
 --> src/main.rs:3:22
  |
3 |     println!("hello");
  |                      ^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
  = note: requested on the command line with `-D clippy::needless-return`
help: remove `return`
  |
3 |     println!("hello")println!("hello");
  |                      ~~~~~~~~~~~~~~~~~~

error: could not compile `poc` (bin "poc" test) due to 1 previous error
```

By expanding macros via `cargo rustc -- -Zunpretty=expanded`, the warning seens correct: the generated code contains a needless "return" expression at trailing position, and there is no explicit `#[allow(..)]`. It seems the `return` here can be trivially removed?

It also reproduces on `#[tokio::test]`.

```rust
   Compiling poc v0.0.0 (/home/oxa/tmp/rust-poc)
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
fn main() {
    let body = async { { ::std::io::_print(format_args!("hello\n")); }; };

    #[allow(clippy :: expect_used, clippy :: diverging_sub_expression)]
    {
        return tokio::runtime::Builder::new_current_thread().enable_all().build().expect("Failed building the Runtime").block_on(body);
    }
}
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
```


Discussion / hints:
As always, this is a clippy bug that warns code generated by external macros (upstream report: https://github.com/rust-lang/rust-clippy/issues/13457). 

(Similar to https://github.com/tokio-rs/tokio/issues/5616, https://github.com/tokio-rs/tokio/issues/3830, etc.)
> As always, this is a clippy bug that warns code generated by external macros (upstream report: [rust-lang/rust-clippy#13457](https://github.com/rust-lang/rust-clippy/issues/13457)).

Thanks for the reference. I also found that the `return` is intentionally introduced from https://github.com/tokio-rs/tokio/pull/4636. Maybe we could still have a `#[allow(..)]` then?
> Maybe we could still have a `#[allow(..)]` then?

Yeah, I would accept a PR to add the corresponding `#[allow()]` attribute to the generated code (as with https://github.com/tokio-rs/tokio/issues/5616#issuecomment-1504887024, https://github.com/tokio-rs/tokio/issues/3830#issuecomment-852117944).

Fix patch:
diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs
index acdc2610f44..184718784e7 100644
--- a/tokio-macros/src/entry.rs
+++ b/tokio-macros/src/entry.rs
@@ -438,8 +438,9 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
     };
 
     let body_ident = quote! { body };
+    // This explicit `return` is intentional. See tokio-rs/tokio#4636
     let last_block = quote_spanned! {last_stmt_end_span=>
-        #[allow(clippy::expect_used, clippy::diverging_sub_expression)]
+        #[allow(clippy::expect_used, clippy::diverging_sub_expression, clippy::needless_return)]
         {
             return #rt
                 .enable_all()


Test patch: