|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::source::{indent_of, snippet}; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{BlockCheckMode, Expr, ExprKind, LoopSource}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::declare_lint_pass; |
| 7 | +use rustc_span::edition::Edition; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Warns when a match, if let, or while let scrutinee is wrapped in a block. |
| 12 | + /// This lint only triggers on the 2021 edition and older. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// It is unusual to write `{ expr }` when you could just have written |
| 16 | + /// `expr`, and it is unlikely that anyone would write that for any reason |
| 17 | + /// other than wanting temporaries in `expr` to be dropped before executing |
| 18 | + /// the body of the `match`/`if let`/`while` statement. However, prior to |
| 19 | + /// the 2024 edition, wrapping the scrutinee in a block did not drop |
| 20 | + /// temporaries before the body executes. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust,ignore |
| 24 | + /// if let Some(x) = { my_function() } { .. } |
| 25 | + /// ``` |
| 26 | + #[clippy::version = "1.98.0"] |
| 27 | + pub BLOCK_SCRUTINEE, |
| 28 | + suspicious, |
| 29 | + "warns when the scrutinee is wrapped in a block in older editions" |
| 30 | +} |
| 31 | + |
| 32 | +declare_lint_pass!(BlockScrutinee => [BLOCK_SCRUTINEE]); |
| 33 | + |
| 34 | +impl<'tcx> LateLintPass<'tcx> for BlockScrutinee { |
| 35 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 36 | + if cx.tcx.sess.edition() >= Edition::Edition2024 { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + let (scrutinee, keyword_fallback) = match expr.kind { |
| 41 | + ExprKind::Match(scrutinee, _, _) => (scrutinee, "`match`"), |
| 42 | + ExprKind::Let(let_expr) => (let_expr.init, "`if let` / `while let`"), |
| 43 | + _ => return, |
| 44 | + }; |
| 45 | + |
| 46 | + if scrutinee.span.from_expansion() || expr.span.from_expansion() { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if let ExprKind::Block(block, _) = scrutinee.kind |
| 51 | + && matches!(block.rules, BlockCheckMode::DefaultBlock) |
| 52 | + && block.stmts.is_empty() |
| 53 | + && let Some(inner_expr) = block.expr |
| 54 | + { |
| 55 | + let inner_snippet = snippet(cx, inner_expr.span, ".."); |
| 56 | + |
| 57 | + let main_msg = "this scrutinee is wrapped in a block"; |
| 58 | + |
| 59 | + span_lint_and_then(cx, BLOCK_SCRUTINEE, scrutinee.span, main_msg, |diag| { |
| 60 | + let mut keyword = keyword_fallback; |
| 61 | + let mut outer_span = expr.span; |
| 62 | + |
| 63 | + if let ExprKind::Let(_) = expr.kind { |
| 64 | + keyword = "`if let`"; |
| 65 | + for (_, node) in cx.tcx.hir_parent_iter(expr.hir_id) { |
| 66 | + if let rustc_hir::Node::Expr(e) = node { |
| 67 | + if let ExprKind::If(..) = e.kind { |
| 68 | + if keyword == "`if let`" { |
| 69 | + outer_span = e.span; |
| 70 | + } |
| 71 | + } else if let ExprKind::Loop(_, _, LoopSource::While, _) = e.kind { |
| 72 | + keyword = "`while let`"; |
| 73 | + outer_span = e.span; |
| 74 | + break; |
| 75 | + } |
| 76 | + } else if matches!(node, rustc_hir::Node::Item(..) | rustc_hir::Node::ImplItem(..)) { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + diag.note(format!( |
| 83 | + "temporary values in this block-wrapped scrutinee will be dropped after the body of the {keyword} statement" |
| 84 | + )); |
| 85 | + |
| 86 | + diag.note("starting with the 2024 edition, temporaries within a block's final expression are dropped immediately at the end of the block"); |
| 87 | + |
| 88 | + let suggestion_msg = format!("to drop temporaries after the surrounding {keyword}, remove the block"); |
| 89 | + |
| 90 | + diag.span_suggestion( |
| 91 | + scrutinee.span, |
| 92 | + suggestion_msg, |
| 93 | + inner_snippet.to_string(), |
| 94 | + Applicability::MaybeIncorrect, |
| 95 | + ); |
| 96 | + |
| 97 | + let indent = indent_of(cx, outer_span).unwrap_or(0); |
| 98 | + let pad = " ".repeat(indent); |
| 99 | + |
| 100 | + diag.multipart_suggestion( |
| 101 | + "to drop temporaries early, move them to a separate local binding (or update your `Cargo.toml` to the 2024 edition)", |
| 102 | + vec![ |
| 103 | + (outer_span.shrink_to_lo(), format!("let res = {inner_snippet};\n{pad}")), |
| 104 | + (scrutinee.span, "res".to_string()), |
| 105 | + ], |
| 106 | + Applicability::MaybeIncorrect, |
| 107 | + ); |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments