Skip to content

Commit 5377dba

Browse files
committed
Fix if_then_some_else_none FP when the then block contains await codes
1 parent 21c5ddd commit 5377dba

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context};
66
use clippy_utils::sugg::Sugg;
77
use clippy_utils::{
8-
as_some_expr, contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context,
9-
is_none_expr, peel_blocks, sym,
8+
as_some_expr, can_move_expr_to_closure, expr_adjustment_requires_coercion, higher, is_else_clause,
9+
is_in_const_context, is_none_expr, peel_blocks, sym,
1010
};
1111
use rustc_errors::Applicability;
1212
use rustc_hir::{Expr, ExprKind};
@@ -76,8 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7676
&& !is_else_clause(cx.tcx, expr)
7777
&& !is_in_const_context(cx)
7878
&& self.msrv.meets(cx, msrvs::BOOL_THEN)
79-
&& !contains_return(then_block.stmts)
80-
&& then_block.expr.is_none_or(|expr| !contains_return(expr))
79+
&& can_move_expr_to_closure(cx, then).is_some()
8180
{
8281
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) {
8382
sym::then_some

clippy_utils/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool
6565
return false;
6666
}
6767
match ty.kind() {
68-
ty::Param(_) => false,
68+
ty::Param(_) | ty::Ref(..) => false,
6969
ty::Adt(def, subs) => def.all_fields().any(|f| !is_copy(cx, f.ty(cx.tcx, subs))),
7070
_ => true,
7171
}

tests/ui/if_then_some_else_none.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,13 @@ mod issue15770 {
218218
Ok(())
219219
}
220220
}
221+
222+
mod issue16176 {
223+
pub async fn foo() -> u32 {
224+
todo!()
225+
}
226+
227+
pub async fn bar(cond: bool) -> Option<u32> {
228+
if cond { Some(foo().await) } else { None } // OK
229+
}
230+
}

tests/ui/if_then_some_else_none.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,13 @@ mod issue15770 {
274274
Ok(())
275275
}
276276
}
277+
278+
mod issue16176 {
279+
pub async fn foo() -> u32 {
280+
todo!()
281+
}
282+
283+
pub async fn bar(cond: bool) -> Option<u32> {
284+
if cond { Some(foo().await) } else { None } // OK
285+
}
286+
}

0 commit comments

Comments
 (0)