]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_reference.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / mut_reference.rs
1 use crate::utils::span_lint;
2 use rustc::hir::*;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::ty::subst::Subst;
5 use rustc::ty::{self, Ty};
6 use rustc::{declare_tool_lint, lint_array};
7
8 declare_clippy_lint! {
9     /// **What it does:** Detects giving 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     /// my_vec.push(&mut value)
20     /// ```
21     pub UNNECESSARY_MUT_PASSED,
22     style,
23     "an argument passed as a mutable reference although the callee only demands an immutable reference"
24 }
25
26 #[derive(Copy, Clone)]
27 pub struct UnnecessaryMutPassed;
28
29 impl LintPass for UnnecessaryMutPassed {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(UNNECESSARY_MUT_PASSED)
32     }
33
34     fn name(&self) -> &'static str {
35         "UnneccessaryMutPassed"
36     }
37 }
38
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnecessaryMutPassed {
40     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
41         match e.node {
42             ExprKind::Call(ref fn_expr, ref arguments) => {
43                 if let ExprKind::Path(ref path) = fn_expr.node {
44                     check_arguments(
45                         cx,
46                         arguments,
47                         cx.tables.expr_ty(fn_expr),
48                         &print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)),
49                     );
50                 }
51             },
52             ExprKind::MethodCall(ref path, _, ref arguments) => {
53                 let def_id = cx.tables.type_dependent_def_id(e.hir_id).unwrap();
54                 let substs = cx.tables.node_substs(e.hir_id);
55                 let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
56                 check_arguments(cx, arguments, method_type, &path.ident.as_str())
57             },
58             _ => (),
59         }
60     }
61 }
62
63 fn check_arguments<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arguments: &[Expr], type_definition: Ty<'tcx>, name: &str) {
64     match type_definition.sty {
65         ty::FnDef(..) | ty::FnPtr(_) => {
66             let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
67             for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
68                 match parameter.sty {
69                     ty::Ref(_, _, MutImmutable)
70                     | ty::RawPtr(ty::TypeAndMut {
71                         mutbl: MutImmutable, ..
72                     }) => {
73                         if let ExprKind::AddrOf(MutMutable, _) = argument.node {
74                             span_lint(
75                                 cx,
76                                 UNNECESSARY_MUT_PASSED,
77                                 argument.span,
78                                 &format!("The function/method `{}` doesn't need a mutable reference", name),
79                             );
80                         }
81                     },
82                     _ => (),
83                 }
84             }
85         },
86         _ => (),
87     }
88 }