|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
1 | 3 | use clippy_config::Conf; |
2 | 4 | use clippy_utils::diagnostics::span_lint_and_then; |
3 | 5 | use clippy_utils::eager_or_lazy::switch_to_eager_eval; |
4 | 6 | use clippy_utils::msrvs::{self, Msrv}; |
5 | 7 | use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context}; |
6 | 8 | use clippy_utils::sugg::Sugg; |
| 9 | +use clippy_utils::visitors::for_each_expr_without_closures; |
7 | 10 | 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, |
| 11 | + as_some_expr, contains_return, desugar_await, expr_adjustment_requires_coercion, higher, is_else_clause, |
| 12 | + is_in_const_context, is_none_expr, peel_blocks, sym, |
10 | 13 | }; |
11 | 14 | use rustc_errors::Applicability; |
12 | 15 | use rustc_hir::{Expr, ExprKind}; |
@@ -77,7 +80,18 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone { |
77 | 80 | && !is_in_const_context(cx) |
78 | 81 | && self.msrv.meets(cx, msrvs::BOOL_THEN) |
79 | 82 | && !contains_return(then_block.stmts) |
80 | | - && then_block.expr.is_none_or(|expr| !contains_return(expr)) |
| 83 | + && then_block.expr.is_none_or(|expr| { |
| 84 | + !contains_return(expr) |
| 85 | + // `bool::then` doesn't work with async code |
| 86 | + && for_each_expr_without_closures(expr, |e| { |
| 87 | + if desugar_await(e).is_some() { |
| 88 | + ControlFlow::Break(()) |
| 89 | + } else { |
| 90 | + ControlFlow::Continue(()) |
| 91 | + } |
| 92 | + }) |
| 93 | + .is_none() |
| 94 | + }) |
81 | 95 | { |
82 | 96 | let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) { |
83 | 97 | sym::then_some |
|
0 commit comments