Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::{
as_some_expr, contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context,
is_none_expr, peel_blocks, sym,
as_some_expr, can_move_expr_to_closure, expr_adjustment_requires_coercion, higher, is_else_clause,
is_in_const_context, is_none_expr, peel_blocks, sym,
};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand Down Expand Up @@ -76,8 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
&& !is_else_clause(cx.tcx, expr)
&& !is_in_const_context(cx)
&& self.msrv.meets(cx, msrvs::BOOL_THEN)
&& !contains_return(then_block.stmts)
&& then_block.expr.is_none_or(|expr| !contains_return(expr))
&& can_move_expr_to_closure(cx, then).is_some()
{
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) {
sym::then_some
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool
return false;
}
match ty.kind() {
ty::Param(_) => false,
ty::Param(_) | ty::Ref(..) => false,
ty::Adt(def, subs) => def.all_fields().any(|f| !is_copy(cx, f.ty(cx.tcx, subs))),
_ => true,
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/if_then_some_else_none.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,13 @@ mod issue15770 {
Ok(())
}
}

mod issue16176 {
pub async fn foo() -> u32 {
todo!()
}

pub async fn bar(cond: bool) -> Option<u32> {
if cond { Some(foo().await) } else { None } // OK
}
}
10 changes: 10 additions & 0 deletions tests/ui/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,13 @@ mod issue15770 {
Ok(())
}
}

mod issue16176 {
pub async fn foo() -> u32 {
todo!()
}

pub async fn bar(cond: bool) -> Option<u32> {
if cond { Some(foo().await) } else { None } // OK
}
}