Skip to content

unnecessary_lazy_evaluations is suggested for async closure #14578

@kb10uy

Description

@kb10uy

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions