Skip to content

Commit 170a6a4

Browse files
committed
fix: unnecessary_lazy_evaluations suggests wrongly for async closure
1 parent ac88357 commit 170a6a4

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

clippy_lints/src/methods/unnecessary_lazy_eval.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{eager_or_lazy, is_from_proc_macro, usage};
55
use hir::FnRetTy;
66
use rustc_errors::Applicability;
7-
use rustc_hir as hir;
7+
use rustc_hir::{self as hir, ClosureKind};
88
use rustc_lint::LateContext;
99
use rustc_span::sym;
1010

@@ -24,7 +24,12 @@ pub(super) fn check<'tcx>(
2424
let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
2525

2626
if (is_option || is_result || is_bool)
27-
&& let hir::ExprKind::Closure(&hir::Closure { body, fn_decl, .. }) = arg.kind
27+
&& let hir::ExprKind::Closure(&hir::Closure {
28+
body,
29+
fn_decl,
30+
kind: ClosureKind::Closure,
31+
..
32+
}) = arg.kind
2833
{
2934
let body = cx.tcx.hir_body(body);
3035
let body_expr = &body.value;

tests/ui/unnecessary_lazy_eval.fixed

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,34 @@ fn panicky_arithmetic_ops(x: usize, y: isize) {
321321
let _x = false.then_some(f1 + f2);
322322
//~^ unnecessary_lazy_evaluations
323323
}
324+
325+
mod issue14578 {
326+
use std::future::Future;
327+
328+
pub struct OptionFuture<F> {
329+
inner: Option<F>,
330+
}
331+
332+
impl<F: Future> Future for OptionFuture<F> {
333+
type Output = Option<F::Output>;
334+
335+
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
336+
todo!()
337+
}
338+
}
339+
340+
impl<T> From<Option<T>> for OptionFuture<T> {
341+
fn from(option: Option<T>) -> Self {
342+
todo!()
343+
}
344+
}
345+
346+
async fn do_something() {
347+
let random_bool = false;
348+
let may_take_while: OptionFuture<_> = random_bool.then(async || take_while(42).await).into();
349+
}
350+
351+
async fn take_while<T>(value: T) -> T {
352+
todo!()
353+
}
354+
}

tests/ui/unnecessary_lazy_eval.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,34 @@ fn panicky_arithmetic_ops(x: usize, y: isize) {
321321
let _x = false.then(|| f1 + f2);
322322
//~^ unnecessary_lazy_evaluations
323323
}
324+
325+
mod issue14578 {
326+
use std::future::Future;
327+
328+
pub struct OptionFuture<F> {
329+
inner: Option<F>,
330+
}
331+
332+
impl<F: Future> Future for OptionFuture<F> {
333+
type Output = Option<F::Output>;
334+
335+
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
336+
todo!()
337+
}
338+
}
339+
340+
impl<T> From<Option<T>> for OptionFuture<T> {
341+
fn from(option: Option<T>) -> Self {
342+
todo!()
343+
}
344+
}
345+
346+
async fn do_something() {
347+
let random_bool = false;
348+
let may_take_while: OptionFuture<_> = random_bool.then(async || take_while(42).await).into();
349+
}
350+
351+
async fn take_while<T>(value: T) -> T {
352+
todo!()
353+
}
354+
}

0 commit comments

Comments
 (0)