Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused async for function with no await statements when function is used in contexts requiring async #9695

Closed
leighmcculloch opened this issue Oct 23, 2022 · 0 comments · Fixed by #11200
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@leighmcculloch
Copy link

leighmcculloch commented Oct 23, 2022

Summary

Functions with the async keyword get the following error, even when the functions must have async to be used in contexts that are async.

unused `async` for function with no await statements

Lint Name

unused_async

Reproducer

I tried this code:

[package]
name = "pro"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = { version = "0.5" }
tokio = { version = "1.0", features = ["full"] }
use axum::{routing::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let router = Router::new().route("/", get(home));
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    axum::Server::bind(&addr)
        .serve(router.into_make_service())
        .await
        .unwrap();
}

async fn home() -> &'static str {
    "Hello, World!"
}

I expected to see this happen:

No warnings.

Instead, this happened:

warning: unused `async` for function with no await statements
  --> src/main.rs:15:1
   |
15 | / async fn home() -> &'static str {
16 | |     "Hello, World!"
17 | | }
   | |_^
   |
   = note: `-W clippy::unused-async` implied by `-W clippy::pedantic`
   = help: consider removing the `async` from this function
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async

Version

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6
@leighmcculloch leighmcculloch added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Oct 23, 2022
bors added a commit that referenced this issue Jul 22, 2023
[`unused_async`]: don't lint if paths reference async fn without immediate call

Fixes #9695
Fixes #9359

Clippy shouldn't lint unused `async` if there are paths referencing them if that path isn't the receiver of a function call, because that means that the function might be passed to some other function:
```rs
async fn f() {} // No await statements, so unused at this point

fn requires_fn_future<F: Future<Output = ()>>(_: fn() -> F) {}
requires_fn_future(f); // `f`'s asyncness is actually not unused.
```
(This isn't limited to just passing the function as a parameter to another function, it could also first be stored in a variable and later passed to another function as an argument)

This requires delaying the linting until post-crate and collecting path references to local async functions along the way.

[`unused_async`]: don't lint if paths reference async fn that require asyncness
@bors bors closed this as completed in e8403a8 Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant