|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::ty::is_copy; |
| 4 | +use clippy_utils::{get_enclosing_block, is_in_test, path_to_local}; |
| 5 | +use rustc_ast::BindingMode; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Block, BorrowKind, Expr, ExprKind, Mutability, Node, PatKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::impl_lint_pass; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for taking a mutable reference on a freshly copied variable due to the use of a block returning a value implementing `Copy`. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// Using a block will make a copy of the block result if its type |
| 17 | + /// implements `Copy`. This might be an indication of a failed attempt |
| 18 | + /// to borrow a variable. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```no_run |
| 22 | + /// # unsafe fn unsafe_func(_: &mut i32) {} |
| 23 | + /// let mut a = 10; |
| 24 | + /// let double_a_ref = &mut unsafe { // Unsafe block needed to call `unsafe_func` |
| 25 | + /// unsafe_func(&mut a); |
| 26 | + /// a |
| 27 | + /// }; |
| 28 | + /// ``` |
| 29 | + /// If you intend to take a reference on `a` and you need the block, |
| 30 | + /// create the reference inside the block instead: |
| 31 | + /// ```no_run |
| 32 | + /// # unsafe fn unsafe_func(_: &mut i32) {} |
| 33 | + /// let mut a = 10; |
| 34 | + /// let double_a_ref = unsafe { // Unsafe block needed to call `unsafe_func` |
| 35 | + /// unsafe_func(&mut a); |
| 36 | + /// &mut a |
| 37 | + /// }; |
| 38 | + /// ``` |
| 39 | + #[clippy::version = "1.90.0"] |
| 40 | + pub MUTABLE_BORROW_OF_COPY, |
| 41 | + suspicious, |
| 42 | + "mutable borrow of a data which was just copied" |
| 43 | +} |
| 44 | + |
| 45 | +pub struct MutableBorrowOfCopy { |
| 46 | + check_in_tests: bool, |
| 47 | +} |
| 48 | + |
| 49 | +impl MutableBorrowOfCopy { |
| 50 | + pub const fn new(conf: &Conf) -> Self { |
| 51 | + Self { |
| 52 | + check_in_tests: conf.check_mutable_borrow_of_copy_in_tests, |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl_lint_pass!(MutableBorrowOfCopy => [MUTABLE_BORROW_OF_COPY]); |
| 58 | + |
| 59 | +impl LateLintPass<'_> for MutableBorrowOfCopy { |
| 60 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 61 | + if !expr.span.from_expansion() |
| 62 | + && let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, sub_expr) = expr.kind |
| 63 | + && let ExprKind::Block(block, _) = sub_expr.kind |
| 64 | + && !block.targeted_by_break |
| 65 | + && block.span.eq_ctxt(expr.span) |
| 66 | + && let Some(block_expr) = block.expr |
| 67 | + && let block_ty = cx.typeck_results().expr_ty_adjusted(block_expr) |
| 68 | + && is_copy(cx, block_ty) |
| 69 | + && is_copied_defined_outside_block(cx, block_expr, block) |
| 70 | + && (self.check_in_tests || !is_in_test(cx.tcx, expr.hir_id)) |
| 71 | + { |
| 72 | + span_lint_and_then( |
| 73 | + cx, |
| 74 | + MUTABLE_BORROW_OF_COPY, |
| 75 | + expr.span, |
| 76 | + "mutable borrow of a value which was just copied", |
| 77 | + |diag| { |
| 78 | + diag.multipart_suggestion( |
| 79 | + "try building the reference inside the block", |
| 80 | + vec![ |
| 81 | + (expr.span.until(block.span), String::new()), |
| 82 | + (block_expr.span.shrink_to_lo(), String::from("&mut ")), |
| 83 | + ], |
| 84 | + Applicability::MaybeIncorrect, |
| 85 | + ); |
| 86 | + }, |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/// Checks if `expr` denotes a mutable variable defined outside `block`. This peels away field |
| 93 | +/// accesses or indexing of such a variable first. |
| 94 | +fn is_copied_defined_outside_block(cx: &LateContext<'_>, mut expr: &Expr<'_>, block: &Block<'_>) -> bool { |
| 95 | + while let ExprKind::Field(base, _) | ExprKind::Index(base, _, _) = expr.kind { |
| 96 | + expr = base; |
| 97 | + } |
| 98 | + if let Some(mut current) = path_to_local(expr) |
| 99 | + && let Node::Pat(pat) = cx.tcx.hir_node(current) |
| 100 | + && matches!(pat.kind, PatKind::Binding(BindingMode::MUT, ..)) |
| 101 | + { |
| 102 | + // Scan enclosing blocks until we find `block` (if so, the local is defined within it), or we loop |
| 103 | + // or can't find blocks anymore. |
| 104 | + loop { |
| 105 | + match get_enclosing_block(cx, current).map(|b| b.hir_id) { |
| 106 | + Some(parent) if parent == block.hir_id => return false, |
| 107 | + Some(parent) if parent != current => current = parent, |
| 108 | + _ => return true, |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + false |
| 113 | +} |
0 commit comments