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

Lint for named, but unused, futures #8126

Open
goffrie opened this issue Dec 15, 2021 · 1 comment
Open

Lint for named, but unused, futures #8126

goffrie opened this issue Dec 15, 2021 · 1 comment
Labels
A-lint Area: New lints

Comments

@goffrie
Copy link

goffrie commented Dec 15, 2021

What it does

In some situations, such as async-aware Mutexes, it is useful to call an async function and bind its result to a variable so that it drops at the end of the scope - e.g. let _guard = mutex.lock().await. However, it's probably a mistake to do so without awaiting (or block_on, or otherwise consuming) the Future.

This is a bit rare as you'd usually notice when making use of the returned guard, but not always. This came up today and was a real head-scratcher.

Lint Name

No response

Category

correctness

Advantage

This lint detects an issue that is completely silent at compile time, and hard to detect in tests. (In the mutex example, casual testing may not discover the lack of mutual exclusion at all!)

Drawbacks

This could cause a false positive when calling a (non-async) function that returns a type that implements Future but also has its own nontrivial Drop implementation, that the caller intentionally wants to defer. I'm not aware of any use case where that would apply. In particular, this never applies to an async fn as dropping an async block does nothing except drop its captured arguments.

Example

pub async fn doit(x: std::sync::Arc<tokio::sync::Mutex<()>>) {
    let _foo = x.lock();
    println!("i'm exclusive");
}

Should be instead:

pub async fn doit(x: std::sync::Arc<tokio::sync::Mutex<()>>) {
    let _foo = x.lock().await; // `.await` here
    println!("i'm exclusive");
}
@goffrie goffrie added the A-lint Area: New lints label Dec 15, 2021
@camsteffen
Copy link
Contributor

camsteffen commented Dec 28, 2021

So basically detect a Future assigned to a variable but then never used? Name idea: unused_future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants