From 2dc65397ee5710c16e077e0ca1e7513c365c10a6 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 30 Jan 2021 17:47:51 +0100 Subject: [PATCH] Only store a LocalDefId in hir::Item. Items are guaranteed to be HIR owner. --- clippy_lints/src/copy_iterator.rs | 2 +- clippy_lints/src/derive.rs | 2 +- clippy_lints/src/doc.rs | 7 +++---- clippy_lints/src/empty_enum.rs | 3 +-- clippy_lints/src/exhaustive_items.rs | 2 +- clippy_lints/src/fallible_impl_from.rs | 3 +-- clippy_lints/src/from_over_into.rs | 3 +-- clippy_lints/src/functions.rs | 6 +++--- clippy_lints/src/inherent_impl.rs | 11 +++-------- clippy_lints/src/large_enum_variant.rs | 3 +-- clippy_lints/src/len_zero.rs | 8 +++----- clippy_lints/src/methods/mod.rs | 3 +-- clippy_lints/src/missing_doc.rs | 6 ++---- clippy_lints/src/missing_inline.rs | 2 +- clippy_lints/src/mut_key.rs | 2 +- clippy_lints/src/needless_borrow.rs | 9 +++++---- clippy_lints/src/ptr.rs | 2 +- clippy_lints/src/redundant_pub_crate.rs | 7 +++---- clippy_lints/src/types.rs | 6 ++++-- clippy_lints/src/use_self.rs | 3 +-- clippy_lints/src/utils/inspector.rs | 5 ++--- clippy_lints/src/wildcard_imports.rs | 2 +- 22 files changed, 41 insertions(+), 56 deletions(-) diff --git a/clippy_lints/src/copy_iterator.rs b/clippy_lints/src/copy_iterator.rs index 48899b33899..004bce5f62a 100644 --- a/clippy_lints/src/copy_iterator.rs +++ b/clippy_lints/src/copy_iterator.rs @@ -38,7 +38,7 @@ impl<'tcx> LateLintPass<'tcx> for CopyIterator { .. }) = item.kind { - let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id)); + let ty = cx.tcx.type_of(item.def_id); if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) { span_lint_and_note( diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index b1e363663bb..e8510bde9ad 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -169,7 +169,7 @@ impl<'tcx> LateLintPass<'tcx> for Derive { .. }) = item.kind { - let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id)); + let ty = cx.tcx.type_of(item.def_id); let is_automatically_derived = is_automatically_derived(&*item.attrs); check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived); diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 75e71eb1e4c..c1244858889 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -216,18 +216,17 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown { let headers = check_attrs(cx, &self.valid_idents, &item.attrs); match item.kind { hir::ItemKind::Fn(ref sig, _, body_id) => { - if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id()) + if !(is_entrypoint_fn(cx, item.def_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) { let body = cx.tcx.hir().body(body_id); - let impl_item_def_id = cx.tcx.hir().local_def_id(item.hir_id); let mut fpu = FindPanicUnwrap { cx, - typeck_results: cx.tcx.typeck(impl_item_def_id), + typeck_results: cx.tcx.typeck(item.def_id), panic_span: None, }; fpu.visit_expr(&body.value); - lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id), fpu.panic_span); + lint_for_missing_headers(cx, item.hir_id(), item.span, sig, headers, Some(body_id), fpu.panic_span); } }, hir::ItemKind::Impl(ref impl_) => { diff --git a/clippy_lints/src/empty_enum.rs b/clippy_lints/src/empty_enum.rs index 853b3afdc3a..077c3b75fb8 100644 --- a/clippy_lints/src/empty_enum.rs +++ b/clippy_lints/src/empty_enum.rs @@ -49,9 +49,8 @@ impl<'tcx> LateLintPass<'tcx> for EmptyEnum { return; } - let did = cx.tcx.hir().local_def_id(item.hir_id); if let ItemKind::Enum(..) = item.kind { - let ty = cx.tcx.type_of(did); + let ty = cx.tcx.type_of(item.def_id); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); if adt.variants.is_empty() { span_lint_and_help( diff --git a/clippy_lints/src/exhaustive_items.rs b/clippy_lints/src/exhaustive_items.rs index e3988d0038c..ab9be3398bf 100644 --- a/clippy_lints/src/exhaustive_items.rs +++ b/clippy_lints/src/exhaustive_items.rs @@ -72,7 +72,7 @@ impl LateLintPass<'_> for ExhaustiveItems { fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { if_chain! { if let ItemKind::Enum(..) | ItemKind::Struct(..) = item.kind; - if cx.access_levels.is_exported(item.hir_id); + if cx.access_levels.is_exported(item.hir_id()); if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); then { let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind { diff --git a/clippy_lints/src/fallible_impl_from.rs b/clippy_lints/src/fallible_impl_from.rs index 527905e375d..79828efc206 100644 --- a/clippy_lints/src/fallible_impl_from.rs +++ b/clippy_lints/src/fallible_impl_from.rs @@ -52,10 +52,9 @@ declare_lint_pass!(FallibleImplFrom => [FALLIBLE_IMPL_FROM]); impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { // check for `impl From for ..` - let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id); if_chain! { if let hir::ItemKind::Impl(impl_) = &item.kind; - if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id); + if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.def_id); if cx.tcx.is_diagnostic_item(sym::from_trait, impl_trait_ref.def_id); then { lint_impl_body(cx, item.span, impl_.items); diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index b010abda24d..b644bb07990 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -60,10 +60,9 @@ impl LateLintPass<'_> for FromOverInto { return; } - let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id); if_chain! { if let hir::ItemKind::Impl{ .. } = &item.kind; - if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id); + if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.def_id); if match_def_path(cx, impl_trait_ref.def_id, &INTO); then { diff --git a/clippy_lints/src/functions.rs b/clippy_lints/src/functions.rs index 71a146cc298..28d025315cf 100644 --- a/clippy_lints/src/functions.rs +++ b/clippy_lints/src/functions.rs @@ -283,13 +283,13 @@ impl<'tcx> LateLintPass<'tcx> for Functions { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { let attr = must_use_attr(&item.attrs); if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind { - let is_public = cx.access_levels.is_exported(item.hir_id); + let is_public = cx.access_levels.is_exported(item.hir_id()); let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); if is_public { check_result_unit_err(cx, &sig.decl, item.span, fn_header_span); } if let Some(attr) = attr { - check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr); + check_needless_must_use(cx, &sig.decl, item.hir_id(), item.span, fn_header_span, attr); return; } if is_public && !is_proc_macro(cx.sess(), &item.attrs) && attr_by_name(&item.attrs, "no_mangle").is_none() { @@ -298,7 +298,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions { &sig.decl, cx.tcx.hir().body(*body_id), item.span, - item.hir_id, + item.hir_id(), item.span.with_hi(sig.decl.output.span().hi()), "this function could have a `#[must_use]` attribute", ); diff --git a/clippy_lints/src/inherent_impl.rs b/clippy_lints/src/inherent_impl.rs index ea26c84cde1..005c461f105 100644 --- a/clippy_lints/src/inherent_impl.rs +++ b/clippy_lints/src/inherent_impl.rs @@ -59,20 +59,15 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl { // but filter out implementations that have generic params (type or lifetime) // or are derived from a macro if !in_macro(item.span) && generics.params.is_empty() { - self.impls.insert(item.hir_id.owner.to_def_id(), item.span); + self.impls.insert(item.def_id.to_def_id(), item.span); } } } fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'_>) { - if let Some(item) = krate.items.values().next() { + if !krate.items.is_empty() { // Retrieve all inherent implementations from the crate, grouped by type - for impls in cx - .tcx - .crate_inherent_impls(item.hir_id.owner.to_def_id().krate) - .inherent_impls - .values() - { + for impls in cx.tcx.crate_inherent_impls(def_id::LOCAL_CRATE).inherent_impls.values() { // Filter out implementations that have generic params (type or lifetime) let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def)); if let Some(initial_span) = impl_spans.next() { diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index ad9b4f357a7..ab4cb33612d 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -62,9 +62,8 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant { if in_external_macro(cx.tcx.sess, item.span) { return; } - let did = cx.tcx.hir().local_def_id(item.hir_id); if let ItemKind::Enum(ref def, _) = item.kind { - let ty = cx.tcx.type_of(did); + let ty = cx.tcx.type_of(item.def_id); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); let mut largest_variant: Option<(_, _)> = None; diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index e95caf6a35f..8706cf7f02b 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -177,10 +177,9 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items } } - if cx.access_levels.is_exported(visited_trait.hir_id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) { + if cx.access_levels.is_exported(visited_trait.hir_id()) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) { let mut current_and_super_traits = FxHashSet::default(); - let visited_trait_def_id = cx.tcx.hir().local_def_id(visited_trait.hir_id); - fill_trait_set(visited_trait_def_id.to_def_id(), &mut current_and_super_traits, cx); + fill_trait_set(visited_trait.def_id.to_def_id(), &mut current_and_super_traits, cx); let is_empty_method_found = current_and_super_traits .iter() @@ -230,8 +229,7 @@ fn check_impl_items(cx: &LateContext<'_>, item: &Item<'_>, impl_items: &[ImplIte if let Some(i) = impl_items.iter().find(|i| is_named_self(cx, i, "len")) { if cx.access_levels.is_exported(i.id.hir_id) { - let def_id = cx.tcx.hir().local_def_id(item.hir_id); - let ty = cx.tcx.type_of(def_id); + let ty = cx.tcx.type_of(item.def_id); span_lint( cx, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 0918843294d..a2104b48b4f 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1687,8 +1687,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { let name = impl_item.ident.name.as_str(); let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id); let item = cx.tcx.hir().expect_item(parent); - let def_id = cx.tcx.hir().local_def_id(item.hir_id); - let self_ty = cx.tcx.type_of(def_id); + let self_ty = cx.tcx.type_of(item.def_id); // if this impl block implements a trait, lint in trait definition instead if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind { diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 0e49eaab436..21b59fba995 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -135,8 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { hir::ItemKind::Fn(..) => { // ignore main() if it.ident.name == sym::main { - let def_id = it.hir_id.owner; - let def_key = cx.tcx.hir().def_key(def_id); + let def_key = cx.tcx.hir().def_key(it.def_id); if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) { return; } @@ -159,8 +158,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { | hir::ItemKind::Use(..) => return, }; - let def_id = cx.tcx.hir().local_def_id(it.hir_id); - let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id()); + let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id()); self.check_missing_docs_attrs(cx, &it.attrs, it.span, article, desc); } diff --git a/clippy_lints/src/missing_inline.rs b/clippy_lints/src/missing_inline.rs index 1264813d378..5132bed590a 100644 --- a/clippy_lints/src/missing_inline.rs +++ b/clippy_lints/src/missing_inline.rs @@ -87,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline { return; } - if !cx.access_levels.is_exported(it.hir_id) { + if !cx.access_levels.is_exported(it.hir_id()) { return; } match it.kind { diff --git a/clippy_lints/src/mut_key.rs b/clippy_lints/src/mut_key.rs index 11044e0c2fb..7b9205a99de 100644 --- a/clippy_lints/src/mut_key.rs +++ b/clippy_lints/src/mut_key.rs @@ -57,7 +57,7 @@ declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]); impl<'tcx> LateLintPass<'tcx> for MutableKeyType { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { if let hir::ItemKind::Fn(ref sig, ..) = item.kind { - check_sig(cx, item.hir_id, &sig.decl); + check_sig(cx, item.hir_id(), &sig.decl); } } diff --git a/clippy_lints/src/needless_borrow.rs b/clippy_lints/src/needless_borrow.rs index f1c06692e30..1453ea6e897 100644 --- a/clippy_lints/src/needless_borrow.rs +++ b/clippy_lints/src/needless_borrow.rs @@ -5,11 +5,12 @@ use crate::utils::{is_automatically_derived, snippet_opt, span_lint_and_then}; use if_chain::if_chain; use rustc_errors::Applicability; -use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, HirId, Item, Mutability, Pat, PatKind}; +use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, Item, Mutability, Pat, PatKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; use rustc_middle::ty::adjustment::{Adjust, Adjustment}; use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::def_id::LocalDefId; declare_clippy_lint! { /// **What it does:** Checks for address of operations (`&`) that are going to @@ -35,7 +36,7 @@ declare_clippy_lint! { #[derive(Default)] pub struct NeedlessBorrow { - derived_item: Option, + derived_item: Option, } impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]); @@ -117,13 +118,13 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow { fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) { if is_automatically_derived(item.attrs) { debug_assert!(self.derived_item.is_none()); - self.derived_item = Some(item.hir_id); + self.derived_item = Some(item.def_id); } } fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) { if let Some(id) = self.derived_item { - if item.hir_id == id { + if item.def_id == id { self.derived_item = None; } } diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index b5aa3411140..9efeac3d823 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -124,7 +124,7 @@ declare_lint_pass!(Ptr => [PTR_ARG, CMP_NULL, MUT_FROM_REF]); impl<'tcx> LateLintPass<'tcx> for Ptr { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { if let ItemKind::Fn(ref sig, _, body_id) = item.kind { - check_fn(cx, &sig.decl, item.hir_id, Some(body_id)); + check_fn(cx, &sig.decl, item.hir_id(), Some(body_id)); } } diff --git a/clippy_lints/src/redundant_pub_crate.rs b/clippy_lints/src/redundant_pub_crate.rs index acd9047ace6..c876bae2303 100644 --- a/clippy_lints/src/redundant_pub_crate.rs +++ b/clippy_lints/src/redundant_pub_crate.rs @@ -42,11 +42,10 @@ impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]); impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { if let VisibilityKind::Crate { .. } = item.vis.node { - if !cx.access_levels.is_exported(item.hir_id) { + if !cx.access_levels.is_exported(item.hir_id()) { if let Some(false) = self.is_exported.last() { let span = item.span.with_hi(item.ident.span.hi()); - let def_id = cx.tcx.hir().local_def_id(item.hir_id); - let descr = cx.tcx.def_kind(def_id).descr(def_id.to_def_id()); + let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id()); span_lint_and_then( cx, REDUNDANT_PUB_CRATE, @@ -66,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { } if let ItemKind::Mod { .. } = item.kind { - self.is_exported.push(cx.access_levels.is_exported(item.hir_id)); + self.is_exported.push(cx.access_levels.is_exported(item.hir_id())); } } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 58af5b12c37..05754503163 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1106,7 +1106,9 @@ fn is_empty_block(expr: &Expr<'_>) -> bool { expr.kind, ExprKind::Block( Block { - stmts: &[], expr: None, .. + stmts: &[], + expr: None, + .. }, _, ) @@ -2565,7 +2567,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher { } } - if !cx.access_levels.is_exported(item.hir_id) { + if !cx.access_levels.is_exported(item.hir_id()) { return; } diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 72d1ca73929..f2ff8c959c2 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -196,8 +196,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { item_path, cx, }; - let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id); - let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id); + let impl_trait_ref = cx.tcx.impl_trait_ref(item.def_id); if let Some(impl_trait_ref) = impl_trait_ref { for impl_item_ref in impl_.items { diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index b52083af6fd..9c1d98cd707 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -370,7 +370,7 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) { } fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) { - let did = cx.tcx.hir().local_def_id(item.hir_id); + let did = item.def_id; println!("item `{}`", item.ident.name); match item.vis.node { hir::VisibilityKind::Public => println!("public"), @@ -383,8 +383,7 @@ fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) { } match item.kind { hir::ItemKind::ExternCrate(ref _renamed_from) => { - let def_id = cx.tcx.hir().local_def_id(item.hir_id); - if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(def_id) { + if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(did) { let source = cx.tcx.used_crate_source(crate_id); if let Some(ref src) = source.dylib { println!("extern crate dylib source: {:?}", src.0); diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index 10005a7fc81..094b1a42346 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -113,7 +113,7 @@ impl LateLintPass<'_> for WildcardImports { if_chain! { if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind; if self.warn_on_all || !self.check_exceptions(item, use_path.segments); - let used_imports = cx.tcx.names_imported_by_glob_use(item.hir_id.owner); + let used_imports = cx.tcx.names_imported_by_glob_use(item.def_id); if !used_imports.is_empty(); // Already handled by `unused_imports` then { let mut applicability = Applicability::MachineApplicable;