X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Ftrivially_copy_pass_by_ref.rs;h=0f00ed6c1fadd94229e05d3452d1890e3ec1e23c;hb=2ff568d746e4641b992c0b74bea046e43a637997;hp=eb6e46c3014eca6b13d3a95b6e9c5166ddf0f700;hpb=7810652310a1e8d6cea6098b1b92e793e577d587;p=rust.git diff --git a/clippy_lints/src/trivially_copy_pass_by_ref.rs b/clippy_lints/src/trivially_copy_pass_by_ref.rs index eb6e46c3014..0f00ed6c1fa 100644 --- a/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -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_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 @@ -54,6 +53,7 @@ "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, 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, target: &SessionConfig) -> Self { Self { limit } } - fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl, span: Option) { + fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &FnDecl<'_>, span: Option) { let fn_def_id = cx.tcx.hir().local_def_id(hir_id); let fn_sig = cx.tcx.fn_sig(fn_def_id); @@ -82,7 +82,7 @@ fn check_poly_fn(&mut self, cx: &LateContext<'_, 'tcx>, hir_id: HirId, decl: &Fn // Use lifetimes to determine if we're returning a reference to the // argument. In that case we can't switch to pass-by-value as the // argument will not live long enough. - let output_lts = match fn_sig.output().sty { + let output_lts = match fn_sig.output().kind { ty::Ref(output_lt, _, _) => vec![output_lt], ty::Adt(_, substs) => substs.regions().collect(), _ => vec![], @@ -96,12 +96,12 @@ 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.sty; + 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()); if size <= self.limit; - if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.node; + if let hir::TyKind::Rptr(_, MutTy { ty: ref decl_ty, .. }) = input.kind; then { let value_type = if is_self_ty(decl_ty) { "self".into() @@ -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.node { + 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, ) { @@ -166,7 +166,7 @@ fn check_fn( // 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.node, ItemKind::Impl(_, _, _, _, Some(_), _, _) | + if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } | ItemKind::Trait(..)) { return;