|
| 1 | +use crate::utils::{snippet, span_lint_and_then}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{ |
| 5 | + BindingAnnotation, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, Local, Node, PatKind, QPath, TraitFn, |
| 6 | + TraitItem, TraitItemKind, |
| 7 | +}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// **What it does:** Checks for function arguments declared as not mutable and |
| 14 | + /// later rebound as mutable. |
| 15 | + /// |
| 16 | + /// **Why is this bad?** It can be easily improved by just declaring the function |
| 17 | + /// argument as mutable and removing the unnecessary assignment. |
| 18 | + /// |
| 19 | + /// **Known problems:** The function argument might have been shadowed by another |
| 20 | + /// value before the assignment. |
| 21 | + /// |
| 22 | + /// **Example:** |
| 23 | + /// |
| 24 | + /// ```rust |
| 25 | + /// fn foo_bad(bar: Vec<i32>) -> Vec<i32> { |
| 26 | + /// let mut bar = bar; |
| 27 | + /// bar.push(42); |
| 28 | + /// bar |
| 29 | + /// } |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```rust |
| 33 | + /// fn foo(mut bar: Vec<i32>) -> Vec<i32> { |
| 34 | + /// bar.push(42); |
| 35 | + /// bar |
| 36 | + /// } |
| 37 | + /// ``` |
| 38 | + pub REBIND_FN_ARG_AS_MUT, |
| 39 | + style, |
| 40 | + "non-mutable function argument rebound as mutable" |
| 41 | +} |
| 42 | + |
| 43 | +declare_lint_pass!(RebindFnArgAsMut => [REBIND_FN_ARG_AS_MUT]); |
| 44 | + |
| 45 | +impl LateLintPass<'_> for RebindFnArgAsMut { |
| 46 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local: &Local<'tcx>) { |
| 47 | + if_chain! { |
| 48 | + if !in_external_macro(cx.tcx.sess, local.span); |
| 49 | + |
| 50 | + // LHS |
| 51 | + if let PatKind::Binding(BindingAnnotation::Mutable, _, name, None) = local.pat.kind; |
| 52 | + |
| 53 | + // RHS |
| 54 | + if let Some(init) = local.init; |
| 55 | + if let ExprKind::Path(QPath::Resolved(_, path)) = init.kind; |
| 56 | + if path.segments.len() == 1; |
| 57 | + if path.segments[0].ident == name; |
| 58 | + |
| 59 | + if let rustc_hir::def::Res::Local(id) = path.res; |
| 60 | + |
| 61 | + // Fn |
| 62 | + let parent_id = cx.tcx.hir().get_parent_item(id); |
| 63 | + |
| 64 | + if let Node::Item(&Item { kind: ItemKind::Fn(_, _, body_id), .. }) |
| 65 | + | Node::ImplItem(&ImplItem { kind: ImplItemKind::Fn(_, body_id), .. }) |
| 66 | + | Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)), .. }) |
| 67 | + = cx.tcx.hir().get(parent_id); |
| 68 | + |
| 69 | + let body = cx.tcx.hir().body(body_id); |
| 70 | + if let Some(param) = body.params.iter().find(|param| param.pat.hir_id == id); |
| 71 | + if let PatKind::Binding(BindingAnnotation::Unannotated, ..) = param.pat.kind; |
| 72 | + |
| 73 | + then { |
| 74 | + span_lint_and_then( |
| 75 | + cx, |
| 76 | + REBIND_FN_ARG_AS_MUT, |
| 77 | + param.pat.span, |
| 78 | + &format!("argument `{}` is declared as not mutable, and later rebound as mutable", name), |
| 79 | + |diag| { |
| 80 | + diag.span_suggestion( |
| 81 | + param.pat.span, |
| 82 | + "consider just declaring as mutable", |
| 83 | + format!("mut {}", snippet(cx, param.pat.span, "_")), |
| 84 | + Applicability::MaybeIncorrect, |
| 85 | + ); |
| 86 | + diag.span_help(local.span, "and remove this"); |
| 87 | + }, |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments