|
| 1 | +use crate::utils; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_hir::{Expr, ExprKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 5 | +use rustc_middle::lint::in_external_macro; |
| 6 | +use rustc_semver::RustcVersion; |
| 7 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 8 | + |
| 9 | +const IF_THEN_SOME_ELSE_NONE_MSRV: RustcVersion = RustcVersion::new(1, 50, 0); |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// **What it does:** Checks for if-else that could be written to `bool::then`. |
| 13 | + /// |
| 14 | + /// **Why is this bad?** Looks a little redundant. Using `bool::then` helps it have less lines of code. |
| 15 | + /// |
| 16 | + /// **Known problems:** None. |
| 17 | + /// |
| 18 | + /// **Example:** |
| 19 | + /// |
| 20 | + /// ```rust |
| 21 | + /// # let v = vec![0]; |
| 22 | + /// let a = if v.is_empty() { |
| 23 | + /// println!("true!"); |
| 24 | + /// Some(42) |
| 25 | + /// } else { |
| 26 | + /// None |
| 27 | + /// }; |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// Could be written: |
| 31 | + /// |
| 32 | + /// ```rust |
| 33 | + /// # let v = vec![0]; |
| 34 | + /// let a = v.is_empty().then(|| { |
| 35 | + /// println!("true!"); |
| 36 | + /// 42 |
| 37 | + /// }); |
| 38 | + /// ``` |
| 39 | + pub IF_THEN_SOME_ELSE_NONE, |
| 40 | + restriction, |
| 41 | + "Finds if-else that could be written using `bool::then`" |
| 42 | +} |
| 43 | + |
| 44 | +pub struct IfThenSomeElseNone { |
| 45 | + msrv: Option<RustcVersion>, |
| 46 | +} |
| 47 | + |
| 48 | +impl IfThenSomeElseNone { |
| 49 | + #[must_use] |
| 50 | + pub fn new(msrv: Option<RustcVersion>) -> Self { |
| 51 | + Self { msrv } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]); |
| 56 | + |
| 57 | +impl LateLintPass<'_> for IfThenSomeElseNone { |
| 58 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) { |
| 59 | + if !utils::meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if in_external_macro(cx.sess(), expr.span) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // We only care about the top-most `if` in the chain |
| 68 | + if utils::parent_node_is_if_expr(expr, cx) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if_chain! { |
| 73 | + if let ExprKind::If(ref cond, ref then, Some(ref els)) = expr.kind; |
| 74 | + if let ExprKind::Block(ref then_block, _) = then.kind; |
| 75 | + if let Some(ref then_expr) = then_block.expr; |
| 76 | + if let ExprKind::Call(ref then_call, [then_arg]) = then_expr.kind; |
| 77 | + if let ExprKind::Path(ref then_call_qpath) = then_call.kind; |
| 78 | + if utils::match_qpath(then_call_qpath, &utils::paths::OPTION_SOME); |
| 79 | + if let ExprKind::Block(ref els_block, _) = els.kind; |
| 80 | + if els_block.stmts.is_empty(); |
| 81 | + if let Some(ref els_expr) = els_block.expr; |
| 82 | + if let ExprKind::Path(ref els_call_qpath) = els_expr.kind; |
| 83 | + if utils::match_qpath(els_call_qpath, &utils::paths::OPTION_NONE); |
| 84 | + then { |
| 85 | + let cond_snip = utils::snippet_with_macro_callsite(cx, cond.span, "[condition]"); |
| 86 | + let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) { |
| 87 | + format!("({})", cond_snip) |
| 88 | + } else { |
| 89 | + cond_snip.into_owned() |
| 90 | + }; |
| 91 | + let arg_snip = utils::snippet_with_macro_callsite(cx, then_arg.span, ""); |
| 92 | + let closure_body = if then_block.stmts.is_empty() { |
| 93 | + arg_snip.into_owned() |
| 94 | + } else { |
| 95 | + format!("{{ /* snippet */ {} }}", arg_snip) |
| 96 | + }; |
| 97 | + let help = format!( |
| 98 | + "consider using `bool::then` like: `{}.then(|| {})`", |
| 99 | + cond_snip, |
| 100 | + closure_body, |
| 101 | + ); |
| 102 | + utils::span_lint_and_help( |
| 103 | + cx, |
| 104 | + IF_THEN_SOME_ELSE_NONE, |
| 105 | + expr.span, |
| 106 | + "this could be simplified with `bool::then`", |
| 107 | + None, |
| 108 | + &help, |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + extract_msrv_attr!(LateContext); |
| 115 | +} |
0 commit comments