1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::eager_or_lazy::switch_to_eager_eval;
3 use clippy_utils::msrvs::{self, Msrv};
4 use clippy_utils::source::snippet_with_macro_callsite;
5 use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
6 use rustc_hir::LangItem::{OptionNone, OptionSome};
7 use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
8 use rustc_lint::{LateContext, LateLintPass, LintContext};
9 use rustc_middle::lint::in_external_macro;
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
12 declare_clippy_lint! {
14 /// Checks for if-else that could be written using either `bool::then` or `bool::then_some`.
16 /// ### Why is this bad?
17 /// Looks a little redundant. Using `bool::then` is more concise and incurs no loss of clarity.
18 /// For simple calculations and known values, use `bool::then_some`, which is eagerly evaluated
19 /// in comparison to `bool::then`.
23 /// # let v = vec![0];
24 /// let a = if v.is_empty() {
25 /// println!("true!");
35 /// # let v = vec![0];
36 /// let a = v.is_empty().then(|| {
37 /// println!("true!");
41 #[clippy::version = "1.53.0"]
42 pub IF_THEN_SOME_ELSE_NONE,
44 "Finds if-else that could be written using either `bool::then` or `bool::then_some`"
47 pub struct IfThenSomeElseNone {
51 impl IfThenSomeElseNone {
53 pub fn new(msrv: Msrv) -> Self {
58 impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);
60 impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
61 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
62 if !self.msrv.meets(msrvs::BOOL_THEN) {
66 if in_external_macro(cx.sess(), expr.span) {
70 // We only care about the top-most `if` in the chain
71 if is_else_clause(cx.tcx, expr) {
75 if let Some(higher::If { cond, then, r#else: Some(els) }) = higher::If::hir(expr)
76 && let ExprKind::Block(then_block, _) = then.kind
77 && let Some(then_expr) = then_block.expr
78 && let ExprKind::Call(then_call, [then_arg]) = then_expr.kind
79 && is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
80 && is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
81 && !stmts_contains_early_return(then_block.stmts)
83 let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
84 let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
85 format!("({cond_snip})")
87 cond_snip.into_owned()
89 let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
90 let mut method_body = if then_block.stmts.is_empty() {
93 format!("{{ /* snippet */ {arg_snip} }}")
95 let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(msrvs::BOOL_THEN_SOME) {
98 method_body.insert_str(0, "|| ");
103 "consider using `bool::{method_name}` like: `{cond_snip}.{method_name}({method_body})`",
107 IF_THEN_SOME_ELSE_NONE,
109 &format!("this could be simplified with `bool::{method_name}`"),
116 extract_msrv_attr!(LateContext);
119 fn stmts_contains_early_return(stmts: &[Stmt<'_>]) -> bool {
120 stmts.iter().any(|stmt| {
121 let Stmt { kind: StmtKind::Semi(e), .. } = stmt else { return false };