Rust-clippy: Clippy generating non-user-code warning when using generator implemented futures.

Created on 23 Mar 2020  路  10Comments  路  Source: rust-lang/rust-clippy

I ran this code under Clippy:

#![warn(clippy::pedantic)]

use std::error::Error;

async fn foo() {
    eprintln!("Done.");
}

async fn bar() {
    foo().await;
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    bar().await;

    Ok(())
}

I expected to see this happen: no lints generated.

Instead, this happened: Clippy produced this lint:

warning: used binding `_task_context` which is prefixed with an underscore. A leading underscore signals that a binding will not be used.
  --> src/main.rs:10:5
   |
10 |     foo().await;
   |     ^^^^^^^^^^^
   |

If you want to reproduce, you can use the Clippy tool on the playground here: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d343f2d15d5de2e2ac222c73a9680a0b

L-bug T-async-await good-first-issue

Most helpful comment

For anyone subscribed to this issue, this fix was released in today's stable 1.45.0 release.

All 10 comments

A solution could be to apply #[allow(unused)] directly in front of the offending variable, and remove the leading underscore, like so: ...#[allow(unused)] task_context....

(Just passing through)

This warning appears reasonable to me. If I understand it correctly, it is caused by foo not actually making use of any async function. Hence there is no point in declaring it async.

(Just passing through)

This warning appears reasonable to me. If I understand it correctly, it is caused by foo not actually making use of any async function. Hence there is no point in declaring it async.

That's not correct, the lint is triggering due to the generator code that is used to implement async/await, that code uses a variable that starts with an underscore. My proposed fix would be to remove the underscore, and allow the warning that would trigger if that variable was never used.

My proposed fix would be to remove the underscore, and allow the warning that would trigger if that variable was never used.

Did you raise this on the main rust repo? If not, do that and cc jonas-schievink. Asking for it in the clippy repo will not help.

I did do that, it was moved here for some reason though.

@jonas-schievink I know you intentionally made it _task_context to suppress rustc's unused lint, but marking it with #[allow(unused)] would do that and also suppress the clippy lint being discussed here. Does it make sense to do that? Or would rather have clippy change to ignore it?

While I did mainly choose _task_context because it's much easier to create during HIR lowering than a lint attribute, IMO Clippy should never warn on macro- and lowering-generated code the user can't actually fix. Otherwise you'd ask every compiler contributor and macro author to anticipate any possible Clippy lint and to not make the compiler/macro generate code that trips it, which seems like an unrealistic goal.

Doesn't Clippy already ignore macro- and lowering-generated code in some lints? If so, why not ensure and test that all lints do this?

Yeah, this is definitely a Clippy bug, not a rustc bug. The fix is pretty easy. Just add a check if this comes from an await expansion.

Just add a check if this comes from an await expansion.

As long as it continues to fire for something like ({ let _i = 5; some_future }).await, sure.

For anyone subscribed to this issue, this fix was released in today's stable 1.45.0 release.

Was this page helpful?
0 / 5 - 0 ratings