]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_reference.rs
Rustup to *rustc 1.20.0-nightly (d84693b93 2017-07-09)*
[rust.git] / clippy_lints / src / mut_reference.rs
1 use rustc::lint::*;
2 use rustc::ty::{self, Ty};
3 use rustc::ty::subst::Subst;
4 use rustc::hir::*;
5 use utils::span_lint;
6
7 /// **What it does:** Detects giving a mutable reference to a function that only
8 /// requires an immutable reference.
9 ///
10 /// **Why is this bad?** The immutable reference rules out all other references
11 /// to the value. Also the code misleads about the intent of the call site.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// my_vec.push(&mut value)
18 /// ```
19 declare_lint! {
20     pub UNNECESSARY_MUT_PASSED,
21     Warn,
22     "an argument passed as a mutable reference although the callee only demands an \
23      immutable reference"
24 }
25
26
27 #[derive(Copy,Clone)]
28 pub struct UnnecessaryMutPassed;
29
30 impl LintPass for UnnecessaryMutPassed {
31     fn get_lints(&self) -> LintArray {
32         lint_array!(UNNECESSARY_MUT_PASSED)
33     }
34 }
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnecessaryMutPassed {
37     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
38         match e.node {
39             ExprCall(ref fn_expr, ref arguments) => {
40                 if let ExprPath(ref path) = fn_expr.node {
41                     check_arguments(cx,
42                                     arguments,
43                                     cx.tables.expr_ty(fn_expr),
44                                     &print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)));
45                 }
46             },
47             ExprMethodCall(ref path, _, ref arguments) => {
48                 let def_id = cx.tables.type_dependent_defs[&e.id].def_id();
49                 let substs = cx.tables.node_substs(e.id);
50                 let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
51                 check_arguments(cx, arguments, method_type, &path.name.as_str())
52             },
53             _ => (),
54         }
55     }
56 }
57
58 fn check_arguments<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arguments: &[Expr], type_definition: Ty<'tcx>, name: &str) {
59     match type_definition.sty {
60         ty::TyFnDef(..) | ty::TyFnPtr(_) => {
61             let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
62             for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
63                 match parameter.sty {
64                     ty::TyRef(_, ty::TypeAndMut { mutbl: MutImmutable, .. }) |
65                     ty::TyRawPtr(ty::TypeAndMut { mutbl: MutImmutable, .. }) => {
66                         if let ExprAddrOf(MutMutable, _) = argument.node {
67                             span_lint(cx,
68                                       UNNECESSARY_MUT_PASSED,
69                                       argument.span,
70                                       &format!("The function/method `{}` doesn't need a mutable reference", name));
71                         }
72                     },
73                     _ => (),
74                 }
75             }
76         },
77         _ => (),
78     }
79 }