|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::visitors::{for_each_expr, Descend}; |
| 3 | +use clippy_utils::{higher, meets_msrv, msrvs, peel_blocks}; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_hir::{Expr, ExprKind, Pat, QPath, Stmt, StmtKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 7 | +use rustc_middle::lint::in_external_macro; |
| 8 | +use rustc_semver::RustcVersion; |
| 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 10 | +use std::ops::ControlFlow; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// |
| 15 | + /// Warn of cases where `let...else` could be used |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// |
| 19 | + /// `let...else` provides a standard construct for this pattern |
| 20 | + /// that people can easily recognize. It's also more compact. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// |
| 24 | + /// ```rust |
| 25 | + /// # let w = Some(0); |
| 26 | + /// let v = if let Some(v) = w { v } else { return }; |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// Could be written: |
| 30 | + /// |
| 31 | + /// ```rust |
| 32 | + /// # #![feature(let_else)] |
| 33 | + /// # fn main () { |
| 34 | + /// # let w = Some(0); |
| 35 | + /// let Some(v) = w else { return }; |
| 36 | + /// # } |
| 37 | + /// ``` |
| 38 | + #[clippy::version = "1.60.0"] |
| 39 | + pub MANUAL_LET_ELSE, |
| 40 | + pedantic, |
| 41 | + "manual implementation of a let...else statement" |
| 42 | +} |
| 43 | + |
| 44 | +pub struct ManualLetElse { |
| 45 | + msrv: Option<RustcVersion>, |
| 46 | +} |
| 47 | + |
| 48 | +impl ManualLetElse { |
| 49 | + #[must_use] |
| 50 | + pub fn new(msrv: Option<RustcVersion>) -> Self { |
| 51 | + Self { msrv } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl_lint_pass!(ManualLetElse => [MANUAL_LET_ELSE]); |
| 56 | + |
| 57 | +impl<'tcx> LateLintPass<'tcx> for ManualLetElse { |
| 58 | + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &'tcx Stmt<'tcx>) { |
| 59 | + if !meets_msrv(self.msrv, msrvs::LET_ELSE) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if in_external_macro(cx.sess(), stmt.span) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if_chain! { |
| 68 | + if let StmtKind::Local(local) = stmt.kind; |
| 69 | + if let Some(init) = local.init; |
| 70 | + if let Some(higher::IfLet { let_pat, let_expr: _, if_then, if_else }) = higher::IfLet::hir(cx, init); |
| 71 | + if if_then_simple_identity(let_pat, if_then); |
| 72 | + if let Some(if_else) = if_else; |
| 73 | + if expr_diverges(cx, if_else); |
| 74 | + then { |
| 75 | + span_lint( |
| 76 | + cx, |
| 77 | + MANUAL_LET_ELSE, |
| 78 | + stmt.span, |
| 79 | + "this could be rewritten as `let else`", |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + extract_msrv_attr!(LateContext); |
| 86 | +} |
| 87 | + |
| 88 | +fn expr_diverges(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool { |
| 89 | + fn is_never(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool { |
| 90 | + if let Some(ty) = cx.typeck_results().expr_ty_opt(expr) { |
| 91 | + return ty.is_never(); |
| 92 | + } |
| 93 | + false |
| 94 | + } |
| 95 | + // We can't just call is_never on expr and be done, because the type system |
| 96 | + // sometimes coerces the ! type to something different before we can get |
| 97 | + // our hands on it. So instead, we do a manual search. We do fall back to |
| 98 | + // is_never in some places when there is no better alternative. |
| 99 | + for_each_expr(expr, |ex| { |
| 100 | + match ex.kind { |
| 101 | + ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => ControlFlow::Break(()), |
| 102 | + ExprKind::Call(call, _) => { |
| 103 | + if is_never(cx, ex) || is_never(cx, call) { |
| 104 | + return ControlFlow::Break(()); |
| 105 | + } |
| 106 | + ControlFlow::Continue(Descend::Yes) |
| 107 | + }, |
| 108 | + ExprKind::MethodCall(..) => { |
| 109 | + if is_never(cx, ex) { |
| 110 | + return ControlFlow::Break(()); |
| 111 | + } |
| 112 | + ControlFlow::Continue(Descend::Yes) |
| 113 | + }, |
| 114 | + ExprKind::If(if_expr, if_then, if_else) => { |
| 115 | + let else_diverges = if_else.map_or(false, |ex| expr_diverges(cx, ex)); |
| 116 | + let diverges = expr_diverges(cx, if_expr) || (else_diverges && expr_diverges(cx, if_then)); |
| 117 | + if diverges { |
| 118 | + return ControlFlow::Break(()); |
| 119 | + } |
| 120 | + ControlFlow::Continue(Descend::No) |
| 121 | + }, |
| 122 | + ExprKind::Match(match_expr, match_arms, _) => { |
| 123 | + let diverges = |
| 124 | + expr_diverges(cx, match_expr) || match_arms.iter().all(|arm| expr_diverges(cx, arm.body)); |
| 125 | + if diverges { |
| 126 | + return ControlFlow::Break(()); |
| 127 | + } |
| 128 | + ControlFlow::Continue(Descend::No) |
| 129 | + }, |
| 130 | + |
| 131 | + // Don't continue into loops or labeled blocks, as they are breakable, |
| 132 | + // and we'd have to start checking labels. |
| 133 | + ExprKind::Block(_, Some(_)) | ExprKind::Loop(..) => ControlFlow::Continue(Descend::No), |
| 134 | + |
| 135 | + // Default: descend |
| 136 | + _ => ControlFlow::Continue(Descend::Yes), |
| 137 | + } |
| 138 | + }) |
| 139 | + .is_some() |
| 140 | +} |
| 141 | + |
| 142 | +/// Checks if the passed `if_then` is a simple identity |
| 143 | +fn if_then_simple_identity(let_pat: &'_ Pat<'_>, if_then: &'_ Expr<'_>) -> bool { |
| 144 | + // TODO support patterns with multiple bindings and tuples, like: |
| 145 | + // let (foo, bar) = if let (Some(foo), bar) = g() { (foo, bar) } else { ... } |
| 146 | + if_chain! { |
| 147 | + if let ExprKind::Path(QPath::Resolved(_ty, path)) = &peel_blocks(if_then).kind; |
| 148 | + if let [path_seg] = path.segments; |
| 149 | + then { |
| 150 | + let mut pat_bindings = Vec::new(); |
| 151 | + let_pat.each_binding(|_ann, _hir_id, _sp, ident| { |
| 152 | + pat_bindings.push(ident); |
| 153 | + }); |
| 154 | + if let [binding] = &pat_bindings[..] { |
| 155 | + return path_seg.ident == *binding; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + false |
| 160 | +} |
0 commit comments