]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/mut_reference.rs
Auto merge of #73065 - Amanieu:tls-fix, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / mut_reference.rs
1 use crate::utils::span_lint;
2 use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_middle::ty::subst::Subst;
5 use rustc_middle::ty::{self, Ty};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7
8 declare_clippy_lint! {
9     /// **What it does:** Detects passing a mutable reference to a function that only
10     /// requires an immutable reference.
11     ///
12     /// **Why is this bad?** The immutable reference rules out all other references
13     /// to the value. Also the code misleads about the intent of the call site.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```ignore
19     /// // Bad
20     /// my_vec.push(&mut value)
21     ///
22     /// // Good
23     /// my_vec.push(&value)
24     /// ```
25     pub UNNECESSARY_MUT_PASSED,
26     style,
27     "an argument passed as a mutable reference although the callee only demands an immutable reference"
28 }
29
30 declare_lint_pass!(UnnecessaryMutPassed => [UNNECESSARY_MUT_PASSED]);
31
32 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnecessaryMutPassed {
33     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
34         match e.kind {
35             ExprKind::Call(ref fn_expr, ref arguments) => {
36                 if let ExprKind::Path(ref path) = fn_expr.kind {
37                     check_arguments(
38                         cx,
39                         arguments,
40                         cx.tables.expr_ty(fn_expr),
41                         &rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
42                     );
43                 }
44             },
45             ExprKind::MethodCall(ref path, _, ref arguments, _) => {
46                 let def_id = cx.tables.type_dependent_def_id(e.hir_id).unwrap();
47                 let substs = cx.tables.node_substs(e.hir_id);
48                 let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
49                 check_arguments(cx, arguments, method_type, &path.ident.as_str())
50             },
51             _ => (),
52         }
53     }
54 }
55
56 fn check_arguments<'a, 'tcx>(
57     cx: &LateContext<'a, 'tcx>,
58     arguments: &[Expr<'_>],
59     type_definition: Ty<'tcx>,
60     name: &str,
61 ) {
62     match type_definition.kind {
63         ty::FnDef(..) | ty::FnPtr(_) => {
64             let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
65             for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
66                 match parameter.kind {
67                     ty::Ref(_, _, Mutability::Not)
68                     | ty::RawPtr(ty::TypeAndMut {
69                         mutbl: Mutability::Not, ..
70                     }) => {
71                         if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
72                             span_lint(
73                                 cx,
74                                 UNNECESSARY_MUT_PASSED,
75                                 argument.span,
76                                 &format!("The function/method `{}` doesn't need a mutable reference", name),
77                             );
78                         }
79                     },
80                     _ => (),
81                 }
82             }
83         },
84         _ => (),
85     }
86 }