1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::eager_or_lazy::switch_to_eager_eval;
3 use clippy_utils::source::snippet_with_macro_callsite;
4 use clippy_utils::{contains_return, higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs, peel_blocks};
5 use rustc_hir::LangItem::{OptionNone, OptionSome};
6 use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_middle::lint::in_external_macro;
9 use rustc_semver::RustcVersion;
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 {
48 msrv: Option<RustcVersion>,
51 impl IfThenSomeElseNone {
53 pub fn new(msrv: Option<RustcVersion>) -> 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 !meets_msrv(self.msrv, 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 && let ExprKind::Path(ref then_call_qpath) = then_call.kind
80 && is_lang_ctor(cx, then_call_qpath, OptionSome)
81 && let ExprKind::Path(ref qpath) = peel_blocks(els).kind
82 && is_lang_ctor(cx, qpath, OptionNone)
83 && !stmts_contains_early_return(then_block.stmts)
85 let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
86 let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
87 format!("({})", cond_snip)
89 cond_snip.into_owned()
91 let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
92 let mut method_body = if then_block.stmts.is_empty() {
95 format!("{{ /* snippet */ {} }}", arg_snip)
97 let method_name = if switch_to_eager_eval(cx, expr) && meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) {
100 method_body.insert_str(0, "|| ");
105 "consider using `bool::{}` like: `{}.{}({})`",
106 method_name, cond_snip, method_name, method_body,
110 IF_THEN_SOME_ELSE_NONE,
112 &format!("this could be simplified with `bool::{}`", method_name),
119 extract_msrv_attr!(LateContext);
122 fn stmts_contains_early_return(stmts: &[Stmt<'_>]) -> bool {
123 stmts.iter().any(|stmt| {
124 let Stmt { kind: StmtKind::Semi(e), .. } = stmt else { return false };