]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_reference.rs
Remove a span from hir::ExprKind::MethodCall
[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     /// ```ignore
20     /// // Bad
21     /// my_vec.push(&mut value)
22     ///
23     /// // Good
24     /// my_vec.push(&value)
25     /// ```
26     #[clippy::version = "pre 1.29.0"]
27     pub UNNECESSARY_MUT_PASSED,
28     style,
29     "an argument passed as a mutable reference although the callee only demands an immutable reference"
30 }
31
32 declare_lint_pass!(UnnecessaryMutPassed => [UNNECESSARY_MUT_PASSED]);
33
34 impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
35     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
36         match e.kind {
37             ExprKind::Call(fn_expr, arguments) => {
38                 if let ExprKind::Path(ref path) = fn_expr.kind {
39                     check_arguments(
40                         cx,
41                         arguments,
42                         cx.typeck_results().expr_ty(fn_expr),
43                         &rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
44                         "function",
45                     );
46                 }
47             },
48             ExprKind::MethodCall(path, arguments, _) => {
49                 let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
50                 let substs = cx.typeck_results().node_substs(e.hir_id);
51                 let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
52                 check_arguments(cx, arguments, method_type, path.ident.as_str(), "method");
53             },
54             _ => (),
55         }
56     }
57 }
58
59 fn check_arguments<'tcx>(
60     cx: &LateContext<'tcx>,
61     arguments: &[Expr<'_>],
62     type_definition: Ty<'tcx>,
63     name: &str,
64     fn_kind: &str,
65 ) {
66     match type_definition.kind() {
67         ty::FnDef(..) | ty::FnPtr(_) => {
68             let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
69             for (argument, parameter) in iter::zip(arguments, parameters) {
70                 match parameter.kind() {
71                     ty::Ref(_, _, Mutability::Not)
72                     | ty::RawPtr(ty::TypeAndMut {
73                         mutbl: Mutability::Not, ..
74                     }) => {
75                         if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
76                             span_lint(
77                                 cx,
78                                 UNNECESSARY_MUT_PASSED,
79                                 argument.span,
80                                 &format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
81                             );
82                         }
83                     },
84                     _ => (),
85                 }
86             }
87         },
88         _ => (),
89     }
90 }