Skip to content

unnecessary_lazy_evaluations is suggested for async closure #14578

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

Closed
kb10uy opened this issue Apr 10, 2025 · 1 comment · Fixed by #14644
Closed

unnecessary_lazy_evaluations is suggested for async closure #14578

kb10uy opened this issue Apr 10, 2025 · 1 comment · Fixed by #14644
Assignees
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

@kb10uy
Copy link

kb10uy commented Apr 10, 2025

Summary

For async closures, the lint unnecessary_lazy_evaluations is suggested even if that is necessary.
Clearly, the resulting type will change from impl Future<Output = T> to just T, this causes compile error.

Lint Name

unnecessary_lazy_evaluations

Reproducer

For this code,

use std::time::Duration;
use futures::future::OptionFuture;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let random_bool = false;
    let may_take_while: OptionFuture<_> = random_bool.then(async || take_while(42).await).into();
    println!("{:?}", may_take_while.await);
}

async fn take_while<T>(value: T) -> T {
    sleep(Duration::from_secs(5)).await;
    value
}

Clippy suggests this, but this does not compile.

// this is wrong, the argument of then() should be a future
let may_take_while: OptionFuture<_> = random_bool.then(take_while(42).await).into();

If I use an async block instead, it works well (no suggestion).

// clippy won't suggest the lint for async block.
let may_take_while: OptionFuture<_> = random_bool.then(|| async { take_while(42).await }).into();

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-msvc
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

@kb10uy kb10uy 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 Apr 10, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 10, 2025
@profetia
Copy link
Contributor

@rustbot claim

samueltardieu added a commit to samueltardieu/rust-clippy that referenced this issue Apr 11, 2025
github-merge-queue bot pushed a commit that referenced this issue Apr 17, 2025
#14644)

Closes #14578

changelog: [`unnecessary_lazy_evaluations`] fix wrong suggestions for
async closure
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