Skip to content

Clippy warns "unneeded return statement" for #[tokio::{main,test}] fns on latest nightly #6869

@oxalica

Description

@oxalica

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:

// lib.rs
#[tokio::main(flavor = "current_thread")]
async fn main() {
    println!("hello");
}

with

# 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].

   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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-bugCategory: This is a bug.M-macrosModule: macros in the main Tokio crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions