]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/trivially_copy_pass_by_ref.rs
Adapt codebase to the tool_lints
[rust.git] / clippy_lints / src / trivially_copy_pass_by_ref.rs
1 use std::cmp;
2
3 use matches::matches;
4 use rustc::hir;
5 use rustc::hir::*;
6 use rustc::hir::intravisit::FnKind;
7 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
8 use rustc::{declare_tool_lint, lint_array};
9 use if_chain::if_chain;
10 use rustc::ty::TyKind;
11 use rustc::session::config::Config as SessionConfig;
12 use rustc_target::spec::abi::Abi;
13 use rustc_target::abi::LayoutOf;
14 use syntax::ast::NodeId;
15 use syntax_pos::Span;
16 use crate::utils::{in_macro, is_copy, is_self, span_lint_and_sugg, snippet};
17
18 /// **What it does:** Checks for functions taking arguments by reference, where
19 /// the argument type is `Copy` and small enough to be more efficient to always
20 /// pass by value.
21 ///
22 /// **Why is this bad?** In many calling conventions instances of structs will
23 /// be passed through registers if they fit into two or less general purpose
24 /// registers.
25 ///
26 /// **Known problems:** This lint is target register size dependent, it is
27 /// limited to 32-bit to try and reduce portability problems between 32 and
28 /// 64-bit, but if you are compiling for 8 or 16-bit targets then the limit
29 /// will be different.
30 ///
31 /// The configuration option `trivial_copy_size_limit` can be set to override
32 /// this limit for a project.
33 ///
34 /// This lint attempts to allow passing arguments by reference if a reference
35 /// to that argument is returned. This is implemented by comparing the lifetime
36 /// of the argument and return value for equality. However, this can cause
37 /// false positives in cases involving multiple lifetimes that are bounded by
38 /// each other.
39 ///
40 /// **Example:**
41 /// ```rust
42 /// fn foo(v: &u32) {
43 ///     assert_eq!(v, 42);
44 /// }
45 /// // should be
46 /// fn foo(v: u32) {
47 ///     assert_eq!(v, 42);
48 /// }
49 /// ```
50 declare_clippy_lint! {
51     pub TRIVIALLY_COPY_PASS_BY_REF,
52     perf,
53     "functions taking small copyable arguments by reference"
54 }
55
56 pub struct TriviallyCopyPassByRef {
57     limit: u64,
58 }
59
60 impl TriviallyCopyPassByRef {
61     pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
62         let limit = limit.unwrap_or_else(|| {
63             let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
64             // Cap the calculated bit width at 32-bits to reduce
65             // portability problems between 32 and 64-bit targets
66             let bit_width = cmp::min(bit_width, 32);
67             let byte_width = bit_width / 8;
68             // Use a limit of 2 times the register bit width
69             byte_width * 2
70         });
71         Self { limit }
72     }
73 }
74
75 impl LintPass for TriviallyCopyPassByRef {
76     fn get_lints(&self) -> LintArray {
77         lint_array![TRIVIALLY_COPY_PASS_BY_REF]
78     }
79 }
80
81 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
82     fn check_fn(
83         &mut self,
84         cx: &LateContext<'a, 'tcx>,
85         kind: FnKind<'tcx>,
86         decl: &'tcx FnDecl,
87         body: &'tcx Body,
88         span: Span,
89         node_id: NodeId,
90     ) {
91         if in_macro(span) {
92             return;
93         }
94
95         match kind {
96             FnKind::ItemFn(.., header, _, attrs) => {
97                 if header.abi != Abi::Rust {
98                     return;
99                 }
100                 for a in attrs {
101                     if a.meta_item_list().is_some() && a.name() == "proc_macro_derive" {
102                         return;
103                     }
104                 }
105             },
106             FnKind::Method(..) => (),
107             _ => return,
108         }
109
110         // Exclude non-inherent impls
111         if let Some(Node::Item(item)) = cx.tcx.hir.find(cx.tcx.hir.get_parent_node(node_id)) {
112             if matches!(item.node, ItemKind::Impl(_, _, _, _, Some(_), _, _) |
113                 ItemKind::Trait(..))
114             {
115                 return;
116             }
117         }
118
119         let fn_def_id = cx.tcx.hir.local_def_id(node_id);
120
121         let fn_sig = cx.tcx.fn_sig(fn_def_id);
122         let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
123
124         // Use lifetimes to determine if we're returning a reference to the
125         // argument. In that case we can't switch to pass-by-value as the
126         // argument will not live long enough.
127         let output_lts = match fn_sig.output().sty {
128             TyKind::Ref(output_lt, _, _) => vec![output_lt],
129             TyKind::Adt(_, substs) => substs.regions().collect(),
130             _ => vec![],
131         };
132
133         for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) {
134             // All spans generated from a proc-macro invocation are the same...
135             if span == input.span {
136                 return;
137             }
138
139             if_chain! {
140                 if let TyKind::Ref(input_lt, ty, Mutability::MutImmutable) = ty.sty;
141                 if !output_lts.contains(&input_lt);
142                 if is_copy(cx, ty);
143                 if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
144                 if size <= self.limit;
145                 if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
146                 then {
147                     let value_type = if is_self(arg) {
148                         "self".into()
149                     } else {
150                         snippet(cx, decl_ty.span, "_").into()
151                     };
152                     span_lint_and_sugg(
153                         cx,
154                         TRIVIALLY_COPY_PASS_BY_REF,
155                         input.span,
156                         "this argument is passed by reference, but would be more efficient if passed by value",
157                         "consider passing by value instead",
158                         value_type);
159                 }
160             }
161         }
162     }
163 }