Skip to content

Commit 0a48547

Browse files
committed
Return a LocalDefId in get_parent_item.
1 parent d7024ac commit 0a48547

19 files changed

+39
-39
lines changed

clippy_lints/src/escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
7777
}
7878

7979
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
80-
let parent_node = cx.tcx.hir().find(parent_id);
80+
let parent_node = cx.tcx.hir().find_by_def_id(parent_id);
8181

8282
let mut trait_self_ty = None;
8383
if let Some(Node::Item(item)) = parent_node {

clippy_lints/src/exit.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
3434
if let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id();
3535
if match_def_path(cx, def_id, &paths::EXIT);
3636
let parent = cx.tcx.hir().get_parent_item(e.hir_id);
37-
if let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find(parent);
37+
if let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find_by_def_id(parent);
3838
// If the next item up is a function we check if it is an entry point
3939
// and only then emit a linter warning
40-
let def_id = cx.tcx.hir().local_def_id(parent);
41-
if !is_entrypoint_fn(cx, def_id.to_def_id());
40+
if !is_entrypoint_fn(cx, parent.to_def_id());
4241
then {
4342
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
4443
}

clippy_lints/src/functions/must_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
4848
let attr = must_use_attr(attrs);
4949
if let Some(attr) = attr {
5050
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
51-
} else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.hir_id()).is_none() {
51+
} else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.def_id).is_none() {
5252
check_must_use_candidate(
5353
cx,
5454
sig.decl,

clippy_lints/src/functions/result_unit_err.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &hir::ImplItem<'_>) {
2727
if let hir::ImplItemKind::Fn(ref sig, _) = item.kind {
2828
let is_public = cx.access_levels.is_exported(item.def_id);
2929
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
30-
if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() {
30+
if is_public && trait_ref_of_method(cx, item.def_id).is_none() {
3131
check_result_unit_err(cx, sig.decl, item.span, fn_header_span);
3232
}
3333
}

clippy_lints/src/inherent_to_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
116116
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::String);
117117

118118
// Filters instances of to_string which are required by a trait
119-
if trait_ref_of_method(cx, impl_item.hir_id()).is_none();
119+
if trait_ref_of_method(cx, impl_item.def_id).is_none();
120120

121121
then {
122122
show_lint(cx, impl_item);

clippy_lints/src/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
9191

9292
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
9393
if let ImplItemKind::Fn(ref sig, id) = item.kind {
94-
let report_extra_lifetimes = trait_ref_of_method(cx, item.hir_id()).is_none();
94+
let report_extra_lifetimes = trait_ref_of_method(cx, item.def_id).is_none();
9595
check_fn_inner(
9696
cx,
9797
sig.decl,

clippy_lints/src/loops/needless_range_loop.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ pub(super) fn check<'tcx>(
5858

5959
// ensure that the indexed variable was declared before the loop, see #601
6060
if let Some(indexed_extent) = indexed_extent {
61-
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id);
62-
let parent_def_id = cx.tcx.hir().local_def_id(parent_id);
61+
let parent_def_id = cx.tcx.hir().get_parent_item(expr.hir_id);
6362
let region_scope_tree = cx.tcx.region_scope_tree(parent_def_id);
6463
let pat_extent = region_scope_tree.var_scope(pat.hir_id.local_id);
6564
if region_scope_tree.is_subscope_of(indexed_extent, pat_extent) {
@@ -263,8 +262,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
263262
let res = self.cx.qpath_res(seqpath, seqexpr.hir_id);
264263
match res {
265264
Res::Local(hir_id) => {
266-
let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
267-
let parent_def_id = self.cx.tcx.hir().local_def_id(parent_id);
265+
let parent_def_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
268266
let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id);
269267
if index_used_directly {
270268
self.indexed_directly.insert(

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
20532053
return;
20542054
}
20552055
let name = impl_item.ident.name.as_str();
2056-
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
2056+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
20572057
let item = cx.tcx.hir().expect_item(parent);
20582058
let self_ty = cx.tcx.type_of(item.def_id);
20592059

clippy_lints/src/missing_const_for_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
121121
}
122122
},
123123
FnKind::Method(_, sig, ..) => {
124-
if trait_ref_of_method(cx, hir_id).is_some()
124+
if trait_ref_of_method(cx, def_id).is_some()
125125
|| already_const(sig.header)
126126
|| method_accepts_dropable(cx, sig.decl.inputs)
127127
{

clippy_lints/src/mut_key.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
8989

9090
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
9191
if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
92-
if trait_ref_of_method(cx, item.hir_id()).is_none() {
92+
if trait_ref_of_method(cx, item.def_id).is_none() {
9393
check_sig(cx, item.hir_id(), sig.decl);
9494
}
9595
}

clippy_lints/src/new_without_default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
101101
if sig.decl.inputs.is_empty();
102102
if name == sym::new;
103103
if cx.access_levels.is_reachable(impl_item.def_id);
104-
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
104+
let self_def_id = cx.tcx.hir().get_parent_item(id);
105105
let self_ty = cx.tcx.type_of(self_def_id);
106106
if TyS::same_type(self_ty, return_ty(cx, id));
107107
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default);

clippy_lints/src/non_copy_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
280280

281281
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
282282
if let ImplItemKind::Const(hir_ty, body_id) = &impl_item.kind {
283-
let item_def_id = cx.tcx.hir().get_parent_did(impl_item.hir_id());
283+
let item_def_id = cx.tcx.hir().get_parent_item(impl_item.hir_id());
284284
let item = cx.tcx.hir().expect_item(item_def_id);
285285

286286
match &item.kind {

clippy_lints/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
164164
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
165165
if let ImplItemKind::Fn(ref sig, body_id) = item.kind {
166166
let parent_item = cx.tcx.hir().get_parent_item(item.hir_id());
167-
if let Some(Node::Item(it)) = cx.tcx.hir().find(parent_item) {
167+
if let Some(Node::Item(it)) = cx.tcx.hir().find_by_def_id(parent_item) {
168168
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = it.kind {
169169
return; // ignore trait impls
170170
}

clippy_lints/src/self_named_constructors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
5151
_ => return,
5252
}
5353

54-
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
54+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
5555
let item = cx.tcx.hir().expect_item(parent);
5656
let self_ty = cx.tcx.type_of(item.def_id);
5757
let ret_ty = return_ty(cx, impl_item.hir_id());

clippy_lints/src/suspicious_trait_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
6666
// Check for more than one binary operation in the implemented function
6767
// Linting when multiple operations are involved can result in false positives
6868
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
69-
if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn);
69+
if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get_by_def_id(parent_fn);
7070
if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
7171
let body = cx.tcx.hir().body(body_id);
7272
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);

clippy_lints/src/types/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BO
312312

313313
impl<'tcx> LateLintPass<'tcx> for Types {
314314
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
315-
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id))
316-
{
317-
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
318-
} else {
319-
false
320-
};
315+
let is_in_trait_impl =
316+
if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(id)) {
317+
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
318+
} else {
319+
false
320+
};
321321

322322
let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(id));
323323

@@ -353,7 +353,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
353353
match item.kind {
354354
ImplItemKind::Const(ty, _) => {
355355
let is_in_trait_impl = if let Some(hir::Node::Item(item)) =
356-
cx.tcx.hir().find(cx.tcx.hir().get_parent_item(item.hir_id()))
356+
cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()))
357357
{
358358
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
359359
} else {

clippy_lints/src/unused_self.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
4242
if impl_item.span.from_expansion() {
4343
return;
4444
}
45-
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
45+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
4646
let parent_item = cx.tcx.hir().expect_item(parent);
4747
let assoc_item = cx.tcx.associated_item(impl_item.def_id);
4848
if_chain! {

clippy_lints/src/zero_sized_map_values.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
6969

7070
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
7171
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
72-
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) {
72+
let second_parent_id = cx
73+
.tcx
74+
.hir()
75+
.get_parent_item(cx.tcx.hir().local_def_id_to_hir_id(parent_id));
76+
if let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(second_parent_id) {
7377
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
7478
return true;
7579
}

clippy_utils/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_data_structures::fx::FxHashMap;
7070
use rustc_data_structures::unhash::UnhashMap;
7171
use rustc_hir as hir;
7272
use rustc_hir::def::{DefKind, Res};
73-
use rustc_hir::def_id::{CrateNum, DefId};
73+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID};
7474
use rustc_hir::hir_id::{HirIdMap, HirIdSet};
7575
use rustc_hir::intravisit::{walk_expr, ErasedMap, FnKind, NestedVisitorMap, Visitor};
7676
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -90,7 +90,6 @@ use rustc_middle::ty::binding::BindingMode;
9090
use rustc_middle::ty::{layout::IntegerExt, BorrowKind, DefIdTree, Ty, TyCtxt, TypeAndMut, TypeFoldable, UpvarCapture};
9191
use rustc_semver::RustcVersion;
9292
use rustc_session::Session;
93-
use rustc_span::def_id::LocalDefId;
9493
use rustc_span::hygiene::{ExpnKind, MacroKind};
9594
use rustc_span::source_map::original_sp;
9695
use rustc_span::sym;
@@ -216,7 +215,7 @@ pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<
216215
/// ```
217216
pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool {
218217
let parent_id = cx.tcx.hir().get_parent_item(id);
219-
match cx.tcx.hir().get(parent_id) {
218+
match cx.tcx.hir().get_by_def_id(parent_id) {
220219
Node::Item(&Item {
221220
kind: ItemKind::Const(..) | ItemKind::Static(..),
222221
..
@@ -607,12 +606,13 @@ pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> {
607606
/// }
608607
/// }
609608
/// ```
610-
pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx TraitRef<'tcx>> {
609+
pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, def_id: LocalDefId) -> Option<&'tcx TraitRef<'tcx>> {
611610
// Get the implemented trait for the current function
611+
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
612612
let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
613613
if_chain! {
614-
if parent_impl != hir::CRATE_HIR_ID;
615-
if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
614+
if parent_impl != CRATE_DEF_ID;
615+
if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent_impl);
616616
if let hir::ItemKind::Impl(impl_) = &item.kind;
617617
then { return impl_.of_trait.as_ref(); }
618618
}
@@ -1122,14 +1122,13 @@ pub fn is_entrypoint_fn(cx: &LateContext<'_>, def_id: DefId) -> bool {
11221122
/// Returns `true` if the expression is in the program's `#[panic_handler]`.
11231123
pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
11241124
let parent = cx.tcx.hir().get_parent_item(e.hir_id);
1125-
let def_id = cx.tcx.hir().local_def_id(parent).to_def_id();
1126-
Some(def_id) == cx.tcx.lang_items().panic_impl()
1125+
Some(parent.to_def_id()) == cx.tcx.lang_items().panic_impl()
11271126
}
11281127

11291128
/// Gets the name of the item the expression is in, if available.
11301129
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
11311130
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id);
1132-
match cx.tcx.hir().find(parent_id) {
1131+
match cx.tcx.hir().find_by_def_id(parent_id) {
11331132
Some(
11341133
Node::Item(Item { ident, .. })
11351134
| Node::TraitItem(TraitItem { ident, .. })
@@ -1639,7 +1638,7 @@ pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool
16391638
return true;
16401639
}
16411640
prev_enclosing_node = Some(enclosing_node);
1642-
enclosing_node = map.get_parent_item(enclosing_node);
1641+
enclosing_node = map.local_def_id_to_hir_id(map.get_parent_item(enclosing_node));
16431642
}
16441643

16451644
false

0 commit comments

Comments
 (0)