|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{Expr, ExprKind}; |
| 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.80.0"] |
| 27 | + pub BLOCK_SCRUTINEE, |
| 28 | + correctness, |
| 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 = match expr.kind { |
| 41 | + ExprKind::Match(scrutinee, _, _) => scrutinee, |
| 42 | + ExprKind::Let(let_expr) => let_expr.init, |
| 43 | + _ => return, |
| 44 | + }; |
| 45 | + |
| 46 | + if let ExprKind::Block(block, _) = scrutinee.kind |
| 47 | + && block.stmts.is_empty() |
| 48 | + && let Some(inner_expr) = block.expr |
| 49 | + { |
| 50 | + let inner_snippet = snippet(cx, inner_expr.span, ".."); |
| 51 | + |
| 52 | + span_lint_and_then( |
| 53 | + cx, |
| 54 | + BLOCK_SCRUTINEE, |
| 55 | + scrutinee.span, |
| 56 | + "temporary values in this block-wrapped scrutinee will not be dropped until the end of the statement", |
| 57 | + |diag| { |
| 58 | + diag.note("this behavior is specific to Rust editions prior to 2024"); |
| 59 | + diag.note("in Rust 2024, temporaries in a block scrutinee drop immediately before the match arm or block body"); |
| 60 | + diag.help("if you want the temporaries to be dropped early, you can update your `Cargo.toml` to the 2024 edition"); |
| 61 | + |
| 62 | + diag.span_suggestion( |
| 63 | + scrutinee.span, |
| 64 | + "remove the block to yield the same behavior but with cleaner code", |
| 65 | + inner_snippet.to_string(), |
| 66 | + Applicability::MachineApplicable, |
| 67 | + ); |
| 68 | + |
| 69 | + diag.span_suggestion( |
| 70 | + scrutinee.span, |
| 71 | + "if you intended to drop temporaries early, move the expression to a separate local binding", |
| 72 | + format!("{{ let res = {inner_snippet}; res }}"), |
| 73 | + Applicability::MaybeIncorrect, |
| 74 | + ); |
| 75 | + }, |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments