hir: Remove opt_local_def_id_to_hir_id and opt_hir_node_by_def_id

Also replace a few `hir_node()` calls with `hir_node_by_def_id()`
This commit is contained in:
Vadim Petrochenkov 2024-03-14 21:05:06 +03:00
parent 97f2ade8e6
commit f55a04928f
11 changed files with 21 additions and 31 deletions

View File

@ -76,10 +76,9 @@ fn check_fn(
.hir() .hir()
.get_parent_item(cx.tcx.local_def_id_to_hir_id(fn_def_id)) .get_parent_item(cx.tcx.local_def_id_to_hir_id(fn_def_id))
.def_id; .def_id;
let parent_node = cx.tcx.opt_hir_node_by_def_id(parent_id);
let mut trait_self_ty = None; let mut trait_self_ty = None;
if let Some(Node::Item(item)) = parent_node { if let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_id) {
// If the method is an impl for a trait, don't warn. // If the method is an impl for a trait, don't warn.
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind { if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
return; return;

View File

@ -46,7 +46,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
&& let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id() && let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id()
&& cx.tcx.is_diagnostic_item(sym::process_exit, def_id) && cx.tcx.is_diagnostic_item(sym::process_exit, def_id)
&& let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id && let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id
&& let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.opt_hir_node_by_def_id(parent) && let Node::Item(Item{kind: ItemKind::Fn(..), ..}) = cx.tcx.hir_node_by_def_id(parent)
// If the next item up is a function we check if it is an entry point // If the next item up is a function we check if it is an entry point
// and only then emit a linter warning // and only then emit a linter warning
&& !is_entrypoint_fn(cx, parent.to_def_id()) && !is_entrypoint_fn(cx, parent.to_def_id())

View File

@ -92,7 +92,7 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty
.expect("already checked this is adt") .expect("already checked this is adt")
.did() .did()
.as_local() .as_local()
&& let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(local_def_id) && let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_def_id)
&& let hir::ItemKind::Enum(ref def, _) = item.kind && let hir::ItemKind::Enum(ref def, _) = item.kind
{ {
let variants_size = AdtVariantInfo::new(cx, *adt, subst); let variants_size = AdtVariantInfo::new(cx, *adt, subst);

View File

@ -225,10 +225,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let Some(adt_def) = cx.typeck_results().expr_ty_adjusted(recv).ty_adt_def() if let Some(adt_def) = cx.typeck_results().expr_ty_adjusted(recv).ty_adt_def()
&& let Some(field) = adt_def.all_fields().find(|field| field.name == ident.name) && let Some(field) = adt_def.all_fields().find(|field| field.name == ident.name)
&& let Some(local_did) = field.did.as_local() && let Some(local_did) = field.did.as_local()
&& let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(local_did)
&& !cx.tcx.type_of(field.did).skip_binder().is_phantom_data() && !cx.tcx.type_of(field.did).skip_binder().is_phantom_data()
{ {
(hir_id, ident) (cx.tcx.local_def_id_to_hir_id(local_did), ident)
} else { } else {
return; return;
} }

View File

@ -220,7 +220,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx rustc_hir::Item<'tc
&& let self_ty = cx.tcx.type_of(self_path_did).skip_binder().peel_refs() && let self_ty = cx.tcx.type_of(self_path_did).skip_binder().peel_refs()
&& let Some(self_adt) = self_ty.ty_adt_def() && let Some(self_adt) = self_ty.ty_adt_def()
&& let Some(self_def_id) = self_adt.did().as_local() && let Some(self_def_id) = self_adt.did().as_local()
&& let Some(Node::Item(self_item)) = cx.tcx.opt_hir_node_by_def_id(self_def_id) && let Node::Item(self_item) = cx.tcx.hir_node_by_def_id(self_def_id)
// NB: can't call cx.typeck_results() as we are not in a body // NB: can't call cx.typeck_results() as we are not in a body
&& let typeck_results = cx.tcx.typeck_body(*body_id) && let typeck_results = cx.tcx.typeck_body(*body_id)
&& should_lint(cx, typeck_results, block) && should_lint(cx, typeck_results, block)

View File

@ -112,10 +112,7 @@ fn check_closures<'tcx>(
} }
ctx.prev_bind = None; ctx.prev_bind = None;
ctx.prev_move_to_closure.clear(); ctx.prev_move_to_closure.clear();
if let Some(body) = cx if let Some(body) = associated_body(cx.tcx.hir_node_by_def_id(closure))
.tcx
.opt_hir_node_by_def_id(closure)
.and_then(associated_body)
.map(|(_, body_id)| hir.body(body_id)) .map(|(_, body_id)| hir.body(body_id))
{ {
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body); euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);

View File

@ -72,8 +72,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<
if let Some(self_def) = self_ty.ty_adt_def() if let Some(self_def) = self_ty.ty_adt_def()
&& let Some(self_local_did) = self_def.did().as_local() && let Some(self_local_did) = self_def.did().as_local()
&& let self_id = cx.tcx.local_def_id_to_hir_id(self_local_did) && let Node::Item(x) = cx.tcx.hir_node_by_def_id(self_local_did)
&& let Node::Item(x) = cx.tcx.hir_node(self_id)
&& let type_name = x.ident.name.as_str().to_lowercase() && let type_name = x.ident.name.as_str().to_lowercase()
&& (impl_item.ident.name.as_str() == type_name && (impl_item.ident.name.as_str() == type_name
|| impl_item.ident.name.as_str().replace('_', "") == type_name) || impl_item.ident.name.as_str().replace('_', "") == type_name)

View File

@ -95,7 +95,7 @@ fn is_function_allowed(
/// to be considered. /// to be considered.
fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool { fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
matches!( matches!(
cx.tcx.hir_node(cx.tcx.local_def_id_to_hir_id(def_id)), cx.tcx.hir_node_by_def_id(def_id),
Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_) Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_)
) )
} }

View File

@ -321,7 +321,7 @@ fn check_fn(
_: Span, _: Span,
def_id: LocalDefId, def_id: LocalDefId,
) { ) {
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id( let is_in_trait_impl = if let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(
cx.tcx cx.tcx
.hir() .hir()
.get_parent_item(cx.tcx.local_def_id_to_hir_id(def_id)) .get_parent_item(cx.tcx.local_def_id_to_hir_id(def_id))
@ -366,9 +366,9 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
match item.kind { match item.kind {
ImplItemKind::Const(ty, _) => { ImplItemKind::Const(ty, _) => {
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx let is_in_trait_impl = if let hir::Node::Item(item) = cx
.tcx .tcx
.opt_hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id) .hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
{ {
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
} else { } else {

View File

@ -74,7 +74,7 @@ fn check_ty<'tcx>(&mut self, cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>) {
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool { fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
let parent_id = cx.tcx.hir().get_parent_item(hir_id); let parent_id = cx.tcx.hir().get_parent_item(hir_id);
let second_parent_id = cx.tcx.hir().get_parent_item(parent_id.into()).def_id; let second_parent_id = cx.tcx.hir().get_parent_item(parent_id.into()).def_id;
if let Some(Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(second_parent_id) { if let Node::Item(item) = cx.tcx.hir_node_by_def_id(second_parent_id) {
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind { if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
return true; return true;
} }

View File

@ -330,8 +330,7 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
/// Checks if the `def_id` belongs to a function that is part of a trait impl. /// Checks if the `def_id` belongs to a function that is part of a trait impl.
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool { pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id) if let Node::Item(item) = cx.tcx.parent_hir_node(cx.tcx.local_def_id_to_hir_id(def_id))
&& let Node::Item(item) = cx.tcx.parent_hir_node(hir_id)
&& let ItemKind::Impl(imp) = item.kind && let ItemKind::Impl(imp) = item.kind
{ {
imp.of_trait.is_some() imp.of_trait.is_some()
@ -574,12 +573,12 @@ fn local_item_children_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, name: Symb
let hir = tcx.hir(); let hir = tcx.hir();
let root_mod; let root_mod;
let item_kind = match tcx.opt_hir_node_by_def_id(local_id) { let item_kind = match tcx.hir_node_by_def_id(local_id) {
Some(Node::Crate(r#mod)) => { Node::Crate(r#mod) => {
root_mod = ItemKind::Mod(r#mod); root_mod = ItemKind::Mod(r#mod);
&root_mod &root_mod
}, },
Some(Node::Item(item)) => &item.kind, Node::Item(item) => &item.kind,
_ => return Vec::new(), _ => return Vec::new(),
}; };
@ -1254,12 +1253,10 @@ pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
/// Gets the name of the item the expression is in, if available. /// Gets the name of the item the expression is in, if available.
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> { pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id).def_id; let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
match cx.tcx.opt_hir_node_by_def_id(parent_id) { match cx.tcx.hir_node_by_def_id(parent_id) {
Some( Node::Item(Item { ident, .. })
Node::Item(Item { ident, .. }) | Node::TraitItem(TraitItem { ident, .. })
| Node::TraitItem(TraitItem { ident, .. }) | Node::ImplItem(ImplItem { ident, .. }) => Some(ident.name),
| Node::ImplItem(ImplItem { ident, .. }),
) => Some(ident.name),
_ => None, _ => None,
} }
} }
@ -2667,11 +2664,10 @@ pub fn defined_ty(&self, cx: &LateContext<'tcx>) -> Option<DefinedTy<'tcx>> {
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())), .and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
)), )),
Self::Return(id) => { Self::Return(id) => {
let hir_id = cx.tcx.local_def_id_to_hir_id(id.def_id);
if let Node::Expr(Expr { if let Node::Expr(Expr {
kind: ExprKind::Closure(c), kind: ExprKind::Closure(c),
.. ..
}) = cx.tcx.hir_node(hir_id) }) = cx.tcx.hir_node_by_def_id(id.def_id)
{ {
match c.fn_decl.output { match c.fn_decl.output {
FnRetTy::DefaultReturn(_) => None, FnRetTy::DefaultReturn(_) => None,