]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/trivially_copy_pass_by_ref.rs
Merge commit '7ea7cd165ad6705603852771bf82cc2fd6560db5' into clippyup2
[rust.git] / clippy_lints / src / trivially_copy_pass_by_ref.rs
index bdc679c902afad4bd51c38afc7af547ea9fdb4db..8e0cb94317affc2ce1bf3c0e81d3407fda751bf9 100644 (file)
@@ -2,18 +2,17 @@
 
 use crate::utils::{is_copy, is_self_ty, snippet, span_lint_and_sugg};
 use if_chain::if_chain;
-use matches::matches;
-use rustc::hir;
-use rustc::hir::intravisit::FnKind;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::session::config::Config as SessionConfig;
-use rustc::ty;
-use rustc::{declare_tool_lint, impl_lint_pass};
 use rustc_errors::Applicability;
+use rustc_hir as hir;
+use rustc_hir::intravisit::FnKind;
+use rustc_hir::{Body, FnDecl, HirId, ItemKind, MutTy, Mutability, Node};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty;
+use rustc_session::config::Config as SessionConfig;
+use rustc_session::{declare_tool_lint, impl_lint_pass};
+use rustc_span::Span;
 use rustc_target::abi::LayoutOf;
 use rustc_target::spec::abi::Abi;
-use syntax_pos::Span;
 
 declare_clippy_lint! {
     /// **What it does:** Checks for functions taking arguments by reference, where
     /// fn foo(v: u32) {}
     /// ```
     pub TRIVIALLY_COPY_PASS_BY_REF,
-    perf,
+    pedantic,
     "functions taking small copyable arguments by reference"
 }
 
+#[derive(Copy, Clone)]
 pub struct TriviallyCopyPassByRef {
     limit: u64,
 }
@@ -61,7 +61,7 @@ pub struct TriviallyCopyPassByRef {
 impl<'a, 'tcx> TriviallyCopyPassByRef {
     pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
         let limit = limit.unwrap_or_else(|| {
-            let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
+            let bit_width = u64::from(target.ptr_width);
             // Cap the calculated bit width at 32-bits to reduce
             // portability problems between 32 and 64-bit targets
             let bit_width = cmp::min(bit_width, 32);
@@ -73,7 +73,7 @@ pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
         Self { limit }
     }
 
-    fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl, span: Option<Span>) {
+    fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl<'_>, span: Option<Span>) {
         let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
 
         let fn_sig = cx.tcx.fn_sig(fn_def_id);
@@ -96,7 +96,7 @@ fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &Fn
             }
 
             if_chain! {
-                if let ty::Ref(input_lt, ty, Mutability::MutImmutable) = ty.kind;
+                if let ty::Ref(input_lt, ty, Mutability::Not) = ty.kind;
                 if !output_lts.contains(&input_lt);
                 if is_copy(cx, ty);
                 if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
@@ -126,12 +126,12 @@ fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &Fn
 impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
-    fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
+    fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) {
         if item.span.from_expansion() {
             return;
         }
 
-        if let hir::TraitItemKind::Method(method_sig, _) = &item.kind {
+        if let hir::TraitItemKind::Fn(method_sig, _) = &item.kind {
             self.check_poly_fn(cx, item.hir_id, &*method_sig.decl, None);
         }
     }
@@ -140,8 +140,8 @@ fn check_fn(
         &mut self,
         cx: &LateContext<'a, 'tcx>,
         kind: FnKind<'tcx>,
-        decl: &'tcx FnDecl,
-        _body: &'tcx Body,
+        decl: &'tcx FnDecl<'_>,
+        _body: &'tcx Body<'_>,
         span: Span,
         hir_id: HirId,
     ) {
@@ -161,12 +161,12 @@ fn check_fn(
                 }
             },
             FnKind::Method(..) => (),
-            _ => return,
+            FnKind::Closure(..) => return,
         }
 
         // Exclude non-inherent impls
         if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
-            if matches!(item.kind, ItemKind::Impl(_, _, _, _, Some(_), _, _) |
+            if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
                 ItemKind::Trait(..))
             {
                 return;