]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_reference.rs
Merge commit 'd7b5cbf065b88830ca519adcb73fad4c0d24b1c7' into clippyup
[rust.git] / clippy_lints / src / mut_reference.rs
1 use clippy_utils::diagnostics::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 use std::iter;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Detects passing a mutable reference to a function that only
12     /// requires an immutable reference.
13     ///
14     /// ### Why is this bad?
15     /// The mutable reference rules out all other references to
16     /// the value. Also the code misleads about the intent of the call site.
17     ///
18     /// ### Example
19     /// ```rust
20     /// # let mut vec = Vec::new();
21     /// # let mut value = 5;
22     /// vec.push(&mut value);
23     /// ```
24     ///
25     /// Use instead:
26     /// ```rust
27     /// # let mut vec = Vec::new();
28     /// # let value = 5;
29     /// vec.push(&value);
30     /// ```
31     #[clippy::version = "pre 1.29.0"]
32     pub UNNECESSARY_MUT_PASSED,
33     style,
34     "an argument passed as a mutable reference although the callee only demands an immutable reference"
35 }
36
37 declare_lint_pass!(UnnecessaryMutPassed => [UNNECESSARY_MUT_PASSED]);
38
39 impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
40     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
41         match e.kind {
42             ExprKind::Call(fn_expr, arguments) => {
43                 if let ExprKind::Path(ref path) = fn_expr.kind {
44                     check_arguments(
45                         cx,
46                         arguments,
47                         cx.typeck_results().expr_ty(fn_expr),
48                         &rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
49                         "function",
50                     );
51                 }
52             },
53             ExprKind::MethodCall(path, arguments, _) => {
54                 let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
55                 let substs = cx.typeck_results().node_substs(e.hir_id);
56                 let method_type = cx.tcx.bound_type_of(def_id).subst(cx.tcx, substs);
57                 check_arguments(cx, arguments, method_type, path.ident.as_str(), "method");
58             },
59             _ => (),
60         }
61     }
62 }
63
64 fn check_arguments<'tcx>(
65     cx: &LateContext<'tcx>,
66     arguments: &[Expr<'_>],
67     type_definition: Ty<'tcx>,
68     name: &str,
69     fn_kind: &str,
70 ) {
71     match type_definition.kind() {
72         ty::FnDef(..) | ty::FnPtr(_) => {
73             let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
74             for (argument, parameter) in iter::zip(arguments, parameters) {
75                 match parameter.kind() {
76                     ty::Ref(_, _, Mutability::Not)
77                     | ty::RawPtr(ty::TypeAndMut {
78                         mutbl: Mutability::Not, ..
79                     }) => {
80                         if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
81                             span_lint(
82                                 cx,
83                                 UNNECESSARY_MUT_PASSED,
84                                 argument.span,
85                                 &format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
86                             );
87                         }
88                     },
89                     _ => (),
90                 }
91             }
92         },
93         _ => (),
94     }
95 }