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 emitted when trait bounds where function is used require it to be async #9359

Closed
aonkeeper4 opened this issue Aug 21, 2022 · 1 comment · Fixed by #11200
Closed
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@aonkeeper4
Copy link

Summary

When using actix-web, Clippy warned me that the async on one of my handler functions was unused as there was no await. It turned out that the trait bounds on the .to() method of actix-web::route::Route required that the handler function implemented Handler<Args> which requires an async function.

Lint Name

unused_async

Reproducer

I tried this code:

use actix_web::{web, App, HttpServer};
use std::sync::Mutex;

struct AppStateWithCounter {
    counter: Mutex<i32>, // <- Mutex is necessary to mutate safely across threads
}

async fn index(data: web::Data<AppStateWithCounter>) -> String {
    if let Ok(mut counter) = data.counter.lock() { // <- get counter's MutexGuard
        *counter += 1; // <- access counter inside MutexGuard
        format!("Request number: {counter}") // <- response with count
    } else {
        "Unable to access counter".to_string()
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Note: web::Data created _outside_ HttpServer::new closure
    let counter = web::Data::new(AppStateWithCounter { 
        counter: Mutex::new(0),
    });

    HttpServer::new(move || {
        // move counter into the closure
        App::new()
            .app_data(counter.clone()) // <- register the created data
            .route("/", web::get().to(index))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

I saw this happen:

warning: unused `async` for function with no await statements
  --> src\main.rs:8:1
   |
8  | / async fn index(data: web::Data<AppStateWithCounter>) -> String {
9  | |     if let Ok(mut counter) = data.counter.lock() { // <- get counter's MutexGuard
10 | |         *counter += 1; // <- access counter inside MutexGuard
11 | |         format!("Request number: {counter}") // <- response with count
...  |
14 | |     }
15 | | }
   | |_^
   |
   = 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.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: x86_64-pc-windows-gnu
release: 1.63.0
LLVM version: 14.0.5

Additional Labels

@rustbot label + I-suggestion-causes-error

@aonkeeper4 aonkeeper4 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 21, 2022
@rustbot

This comment was marked as off-topic.

@xFrednet xFrednet added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 21, 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-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants