|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::get_enclosing_block; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::def::Res; |
| 6 | +use rustc_hir::{ |
| 7 | + BindingMode, Body, BodyId, ExprKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind, LetStmt, OwnerNode, |
| 8 | + Param, Pat, PatKind, QPath, TraitFn, TraitItem, TraitItemKind, |
| 9 | +}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_session::declare_lint_pass; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for function arguments declared as not mutable and later rebound as mutable. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// It can be easily improved by just declaring the function argument as mutable and |
| 19 | + /// removing the unnecessary assignment. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```no_run |
| 23 | + /// fn foo_bad(bar: Vec<i32>) -> Vec<i32> { |
| 24 | + /// let mut bar = bar; |
| 25 | + /// bar.push(42); |
| 26 | + /// bar |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```no_run |
| 31 | + /// fn foo(mut bar: Vec<i32>) -> Vec<i32> { |
| 32 | + /// bar.push(42); |
| 33 | + /// bar |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + #[clippy::version = "1.96.0"] |
| 37 | + pub FN_ARG_MUT_REBINDINGS, |
| 38 | + style, |
| 39 | + "non-mutable function argument rebound as mutable" |
| 40 | +} |
| 41 | + |
| 42 | +declare_lint_pass!(FnArgMutRebindings => [FN_ARG_MUT_REBINDINGS]); |
| 43 | + |
| 44 | +impl LateLintPass<'_> for FnArgMutRebindings { |
| 45 | + fn check_local(&mut self, cx: &LateContext<'_>, st: &'_ LetStmt<'_>) { |
| 46 | + if !st.span.in_external_macro(cx.tcx.sess.source_map()) |
| 47 | + |
| 48 | + // check let statement binds as mutable |
| 49 | + && let PatKind::Binding(BindingMode::MUT, _, ident, None) = st.pat.kind |
| 50 | + && let Some(init) = st.init |
| 51 | + |
| 52 | + // check let statement binds to variable with same name |
| 53 | + && let ExprKind::Path(QPath::Resolved(_, path)) = init.kind |
| 54 | + && path.segments.len() == 1 |
| 55 | + && path.segments[0].ident == ident |
| 56 | + && let Res::Local(id) = path.res |
| 57 | + |
| 58 | + // check let statement scope is whole function body |
| 59 | + && let Some((_, owner)) = cx.tcx.hir_parent_owner_iter(st.hir_id).next() |
| 60 | + && let Some(body_id) = fn_body_id(&owner) |
| 61 | + && let &Body { params, value } = cx.tcx.hir_body(body_id) |
| 62 | + && let ExprKind::Block(fn_block, _) = value.kind |
| 63 | + && let Some(st_block) = get_enclosing_block(cx, st.hir_id) |
| 64 | + && fn_block.hir_id == st_block.hir_id |
| 65 | + |
| 66 | + // check param declares as immutable |
| 67 | + && let Some(&Param { |
| 68 | + span: pat_span, |
| 69 | + pat: |
| 70 | + Pat { |
| 71 | + kind: PatKind::Binding(BindingMode::NONE, ..), |
| 72 | + .. |
| 73 | + }, |
| 74 | + .. |
| 75 | + }) = params.iter().find(|p| p.pat.hir_id == id) |
| 76 | + { |
| 77 | + span_lint_and_then( |
| 78 | + cx, |
| 79 | + FN_ARG_MUT_REBINDINGS, |
| 80 | + pat_span, |
| 81 | + format!( |
| 82 | + "argument `{}` is declared as not mutable, and later rebound as mutable", |
| 83 | + ident.name |
| 84 | + ), |
| 85 | + |diag| { |
| 86 | + diag.span_suggestion( |
| 87 | + pat_span, |
| 88 | + "consider just declaring as mutable", |
| 89 | + format!("mut {}", snippet(cx, pat_span, "_")), |
| 90 | + Applicability::MaybeIncorrect, |
| 91 | + ); |
| 92 | + diag.span_help(st.span, "and remove this"); |
| 93 | + }, |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn fn_body_id(node: &OwnerNode<'_>) -> Option<BodyId> { |
| 100 | + match node { |
| 101 | + OwnerNode::Item(Item { |
| 102 | + kind: ItemKind::Fn { body: body_id, .. }, |
| 103 | + .. |
| 104 | + }) |
| 105 | + | OwnerNode::ImplItem(ImplItem { |
| 106 | + kind: ImplItemKind::Fn(_, body_id), |
| 107 | + // avoid false-positive: trait-fn can come from external crate |
| 108 | + impl_kind: ImplItemImplKind::Inherent { .. }, |
| 109 | + .. |
| 110 | + }) |
| 111 | + | OwnerNode::TraitItem(TraitItem { |
| 112 | + kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)), |
| 113 | + .. |
| 114 | + }) => Some(*body_id), |
| 115 | + _ => None, |
| 116 | + } |
| 117 | +} |
0 commit comments