Move some methods from tcx.hir() to tcx

Renamings:
- find -> opt_hir_node
- get -> hir_node
- find_by_def_id -> opt_hir_node_by_def_id
- get_by_def_id -> hir_node_by_def_id

Fix rebase changes using removed methods

Use `tcx.hir_node_by_def_id()` whenever possible in compiler

Fix clippy errors

Fix compiler

Apply suggestions from code review

Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>

Add FIXME for `tcx.hir()` returned type about its removal

Simplify with with `tcx.hir_node_by_def_id`
This commit is contained in:
zetanumbers 2023-12-01 05:28:34 -08:00
parent c8213a49bb
commit fe37cc1d97
39 changed files with 59 additions and 60 deletions

View file

@ -62,7 +62,7 @@ impl LateLintPass<'_> for AbsolutePaths {
} = self; } = self;
if !path.span.from_expansion() if !path.span.from_expansion()
&& let Some(node) = cx.tcx.hir().find(hir_id) && let Some(node) = cx.tcx.opt_hir_node(hir_id)
&& !matches!(node, Node::Item(item) if matches!(item.kind, ItemKind::Use(_, _))) && !matches!(node, Node::Item(item) if matches!(item.kind, ItemKind::Use(_, _)))
&& let [first, rest @ ..] = path.segments && let [first, rest @ ..] = path.segments
// Handle `::std` // Handle `::std`

View file

@ -69,7 +69,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv
fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let map = cx.tcx.hir(); let map = cx.tcx.hir();
if let Some(parent_id) = map.opt_parent_id(expr.hir_id) if let Some(parent_id) = map.opt_parent_id(expr.hir_id)
&& let Some(parent) = map.find(parent_id) && let Some(parent) = cx.tcx.opt_hir_node(parent_id)
{ {
let expr = match parent { let expr = match parent {
Node::Block(block) => { Node::Block(block) => {

View file

@ -1090,7 +1090,7 @@ fn report<'tcx>(
if parent_id == data.first_expr.hir_id { if parent_id == data.first_expr.hir_id {
return; return;
} }
(cx.tcx.hir().get(parent_id).expect_expr().span, true) (cx.tcx.hir_node(parent_id).expect_expr().span, true)
} else { } else {
(expr.span, false) (expr.span, false)
}; };

View file

@ -195,7 +195,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
&& let Some(def_id) = trait_ref.trait_def_id() && let Some(def_id) = trait_ref.trait_def_id()
&& cx.tcx.is_diagnostic_item(sym::Default, def_id) && cx.tcx.is_diagnostic_item(sym::Default, def_id)
&& let impl_item_hir = child.id.hir_id() && let impl_item_hir = child.id.hir_id()
&& let Some(Node::ImplItem(impl_item)) = cx.tcx.hir().find(impl_item_hir) && let Some(Node::ImplItem(impl_item)) = cx.tcx.opt_hir_node(impl_item_hir)
&& let ImplItemKind::Fn(_, b) = &impl_item.kind && let ImplItemKind::Fn(_, b) = &impl_item.kind
&& let Body { value: func_expr, .. } = cx.tcx.hir().body(*b) && let Body { value: func_expr, .. } = cx.tcx.hir().body(*b)
&& let &Adt(adt_def, args) = cx.tcx.type_of(item.owner_id).instantiate_identity().kind() && let &Adt(adt_def, args) = cx.tcx.type_of(item.owner_id).instantiate_identity().kind()

View file

@ -42,7 +42,7 @@ impl LateLintPass<'_> for EmptyDrop {
}) = item.kind }) = item.kind
&& trait_ref.trait_def_id() == cx.tcx.lang_items().drop_trait() && trait_ref.trait_def_id() == cx.tcx.lang_items().drop_trait()
&& let impl_item_hir = child.id.hir_id() && let impl_item_hir = child.id.hir_id()
&& let Some(Node::ImplItem(impl_item)) = cx.tcx.hir().find(impl_item_hir) && let Some(Node::ImplItem(impl_item)) = cx.tcx.opt_hir_node(impl_item_hir)
&& let ImplItemKind::Fn(_, b) = &impl_item.kind && let ImplItemKind::Fn(_, b) = &impl_item.kind
&& let Body { value: func_expr, .. } = cx.tcx.hir().body(*b) && let Body { value: func_expr, .. } = cx.tcx.hir().body(*b)
&& let func_expr = peel_blocks(func_expr) && let func_expr = peel_blocks(func_expr)

View file

@ -5,7 +5,7 @@ use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause; use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, TraitRef, Ty}; use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt};
use rustc_session::impl_lint_pass; use rustc_session::impl_lint_pass;
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::kw; use rustc_span::symbol::kw;
@ -76,7 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
.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.hir().find_by_def_id(parent_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 Some(Node::Item(item)) = parent_node {
@ -122,8 +122,8 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
} }
// TODO: Replace with Map::is_argument(..) when it's fixed // TODO: Replace with Map::is_argument(..) when it's fixed
fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool { fn is_argument(tcx: TyCtxt<'_>, id: HirId) -> bool {
match map.find(id) { match tcx.opt_hir_node(id) {
Some(Node::Pat(Pat { Some(Node::Pat(Pat {
kind: PatKind::Binding(..), kind: PatKind::Binding(..),
.. ..
@ -131,7 +131,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
_ => return false, _ => return false,
} }
matches!(map.find_parent(id), Some(Node::Param(_))) matches!(tcx.hir().find_parent(id), Some(Node::Param(_)))
} }
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
@ -154,7 +154,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) { fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
if cmt.place.projections.is_empty() { if cmt.place.projections.is_empty() {
let map = &self.cx.tcx.hir(); let map = &self.cx.tcx.hir();
if is_argument(*map, cmt.hir_id) { if is_argument(self.cx.tcx, cmt.hir_id) {
// Skip closure arguments // Skip closure arguments
let parent_id = map.parent_id(cmt.hir_id); let parent_id = map.parent_id(cmt.hir_id);
if let Some(Node::Expr(..)) = map.find_parent(parent_id) { if let Some(Node::Expr(..)) = map.find_parent(parent_id) {

View file

@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
&& 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.hir().find_by_def_id(parent) && let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.opt_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

@ -111,7 +111,7 @@ fn look_in_block<'tcx, 'hir>(cx: &LateContext<'tcx>, kind: &'tcx ExprKind<'hir>)
// Find id of the local that expr_end_of_block resolves to // Find id of the local that expr_end_of_block resolves to
&& let ExprKind::Path(QPath::Resolved(None, expr_path)) = expr_end_of_block.kind && let ExprKind::Path(QPath::Resolved(None, expr_path)) = expr_end_of_block.kind
&& let Res::Local(expr_res) = expr_path.res && let Res::Local(expr_res) = expr_path.res
&& let Some(Node::Pat(res_pat)) = cx.tcx.hir().find(expr_res) && let Some(Node::Pat(res_pat)) = cx.tcx.opt_hir_node(expr_res)
// Find id of the local we found in the block // Find id of the local we found in the block
&& let PatKind::Binding(BindingAnnotation::NONE, local_hir_id, _ident, None) = local.pat.kind && let PatKind::Binding(BindingAnnotation::NONE, local_hir_id, _ident, None) = local.pat.kind

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.hir().find_by_def_id(local_def_id) && let Some(hir::Node::Item(item)) = cx.tcx.opt_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

@ -248,7 +248,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
// Checking for slice indexing // Checking for slice indexing
&& let parent_id = map.parent_id(expr.hir_id) && let parent_id = map.parent_id(expr.hir_id)
&& let Some(hir::Node::Expr(parent_expr)) = map.find(parent_id) && let Some(hir::Node::Expr(parent_expr)) = cx.tcx.opt_hir_node(parent_id)
&& let hir::ExprKind::Index(_, index_expr, _) = parent_expr.kind && let hir::ExprKind::Index(_, index_expr, _) = parent_expr.kind
&& let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr) && let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr)
&& let Ok(index_value) = index_value.try_into() && let Ok(index_value) = index_value.try_into()
@ -256,7 +256,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
// Make sure that this slice index is read only // Make sure that this slice index is read only
&& let maybe_addrof_id = map.parent_id(parent_id) && let maybe_addrof_id = map.parent_id(parent_id)
&& let Some(hir::Node::Expr(maybe_addrof_expr)) = map.find(maybe_addrof_id) && let Some(hir::Node::Expr(maybe_addrof_expr)) = cx.tcx.opt_hir_node(maybe_addrof_id)
&& let hir::ExprKind::AddrOf(_kind, hir::Mutability::Not, _inner_expr) = maybe_addrof_expr.kind && let hir::ExprKind::AddrOf(_kind, hir::Mutability::Not, _inner_expr) = maybe_addrof_expr.kind
{ {
use_info.index_use.push((index_value, map.span(parent_expr.hir_id))); use_info.index_use.push((index_value, map.span(parent_expr.hir_id)));

View file

@ -122,7 +122,7 @@ fn get_impl_span(cx: &LateContext<'_>, id: LocalDefId) -> Option<Span> {
kind: ItemKind::Impl(impl_item), kind: ItemKind::Impl(impl_item),
span, span,
.. ..
}) = cx.tcx.hir().get(id) }) = cx.tcx.hir_node(id)
{ {
(!span.from_expansion() (!span.from_expansion()
&& impl_item.generics.params.is_empty() && impl_item.generics.params.is_empty()

View file

@ -147,7 +147,7 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
&& let Some(output) = && let Some(output) =
parse_len_output(cx, cx.tcx.fn_sig(item.owner_id).instantiate_identity().skip_binder()) parse_len_output(cx, cx.tcx.fn_sig(item.owner_id).instantiate_identity().skip_binder())
{ {
let (name, kind) = match cx.tcx.hir().find(ty_hir_id) { let (name, kind) = match cx.tcx.opt_hir_node(ty_hir_id) {
Some(Node::ForeignItem(x)) => (x.ident.name, "extern type"), Some(Node::ForeignItem(x)) => (x.ident.name, "extern type"),
Some(Node::Item(x)) => match x.kind { Some(Node::Item(x)) => match x.kind {
ItemKind::Struct(..) => (x.ident.name, "struct"), ItemKind::Struct(..) => (x.ident.name, "struct"),

View file

@ -195,7 +195,7 @@ fn check_fn_inner<'tcx>(
.iter() .iter()
// In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a // In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a
// `Node::GenericParam`. // `Node::GenericParam`.
.filter_map(|&def_id| cx.tcx.hir().get_by_def_id(def_id).ident()) .filter_map(|&def_id| cx.tcx.hir_node_by_def_id(def_id).ident())
.map(|ident| ident.to_string()) .map(|ident| ident.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");

View file

@ -40,7 +40,7 @@ fn mut_warn_with_span(cx: &LateContext<'_>, span: Option<Span>) {
fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId> { fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId> {
if let Some(hir_id) = path_to_local(bound) if let Some(hir_id) = path_to_local(bound)
&& let Node::Pat(pat) = cx.tcx.hir().get(hir_id) && let Node::Pat(pat) = cx.tcx.hir_node(hir_id)
&& let PatKind::Binding(BindingAnnotation::MUT, ..) = pat.kind && let PatKind::Binding(BindingAnnotation::MUT, ..) = pat.kind
{ {
return Some(hir_id); return Some(hir_id);

View file

@ -58,12 +58,12 @@ pub(super) fn check<'tcx>(
match cx.qpath_res(qpath, pushed_item.hir_id) { match cx.qpath_res(qpath, pushed_item.hir_id) {
// immutable bindings that are initialized with literal or constant // immutable bindings that are initialized with literal or constant
Res::Local(hir_id) => { Res::Local(hir_id) => {
let node = cx.tcx.hir().get(hir_id); let node = cx.tcx.hir_node(hir_id);
if let Node::Pat(pat) = node if let Node::Pat(pat) = node
&& let PatKind::Binding(bind_ann, ..) = pat.kind && let PatKind::Binding(bind_ann, ..) = pat.kind
&& !matches!(bind_ann, BindingAnnotation(_, Mutability::Mut)) && !matches!(bind_ann, BindingAnnotation(_, Mutability::Mut))
&& let parent_node = cx.tcx.hir().parent_id(hir_id) && let parent_node = cx.tcx.hir().parent_id(hir_id)
&& let Some(Node::Local(parent_let_expr)) = cx.tcx.hir().find(parent_node) && let Some(Node::Local(parent_let_expr)) = cx.tcx.opt_hir_node(parent_node)
&& let Some(init) = parent_let_expr.init && let Some(init) = parent_let_expr.init
{ {
match init.kind { match init.kind {

View file

@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
&& block.stmts.is_empty() && block.stmts.is_empty()
&& let Some(closure_body) = desugared_async_block(cx, block) && let Some(closure_body) = desugared_async_block(cx, block)
&& let Node::Item(Item {vis_span, ..}) | Node::ImplItem(ImplItem {vis_span, ..}) = && let Node::Item(Item {vis_span, ..}) | Node::ImplItem(ImplItem {vis_span, ..}) =
cx.tcx.hir().get_by_def_id(def_id) cx.tcx.hir_node_by_def_id(def_id)
{ {
let header_span = span.with_hi(ret_ty.span.hi()); let header_span = span.with_hi(ret_ty.span.hi());

View file

@ -76,7 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
// Also ensures the const is nonzero since zero can't be a divisor // Also ensures the const is nonzero since zero can't be a divisor
&& const1 == const2 && const2 == const3 && const1 == const2 && const2 == const3
&& let Some(hir_id) = path_to_local(expr3) && let Some(hir_id) = path_to_local(expr3)
&& let Some(Node::Pat(_)) = cx.tcx.hir().find(hir_id) && let Some(Node::Pat(_)) = cx.tcx.opt_hir_node(hir_id)
{ {
// Apply only to params or locals with annotated types // Apply only to params or locals with annotated types
match cx.tcx.hir().find_parent(hir_id) { match cx.tcx.hir().find_parent(hir_id) {

View file

@ -44,7 +44,7 @@ pub(super) fn check<'tcx>(
// add note if not multi-line // add note if not multi-line
span_lint_and_then(cx, FILTER_NEXT, expr.span, msg, |diag| { span_lint_and_then(cx, FILTER_NEXT, expr.span, msg, |diag| {
let (applicability, pat) = if let Some(id) = path_to_local(recv) let (applicability, pat) = if let Some(id) = path_to_local(recv)
&& let Some(hir::Node::Pat(pat)) = cx.tcx.hir().find(id) && let Some(hir::Node::Pat(pat)) = cx.tcx.opt_hir_node(id)
&& let hir::PatKind::Binding(BindingAnnotation(_, Mutability::Not), _, ident, _) = pat.kind && let hir::PatKind::Binding(BindingAnnotation(_, Mutability::Not), _, ident, _) = pat.kind
{ {
(Applicability::Unspecified, Some((pat.span, ident))) (Applicability::Unspecified, Some((pat.span, ident)))

View file

@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
"called `skip(..).next()` on an iterator", "called `skip(..).next()` on an iterator",
|diag| { |diag| {
if let Some(id) = path_to_local(recv) if let Some(id) = path_to_local(recv)
&& let Node::Pat(pat) = cx.tcx.hir().get(id) && let Node::Pat(pat) = cx.tcx.hir_node(id)
&& let PatKind::Binding(ann, _, _, _) = pat.kind && let PatKind::Binding(ann, _, _, _) = pat.kind
&& ann != BindingAnnotation::MUT && ann != BindingAnnotation::MUT
{ {

View file

@ -135,7 +135,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
fn visit_path(&mut self, path: &Path<'tcx>, _: HirId) { fn visit_path(&mut self, path: &Path<'tcx>, _: HirId) {
if let Res::Local(local_id) = path.res if let Res::Local(local_id) = path.res
&& let Some(Node::Pat(pat)) = self.cx.tcx.hir().find(local_id) && let Some(Node::Pat(pat)) = self.cx.tcx.opt_hir_node(local_id)
&& let PatKind::Binding(_, local_id, ..) = pat.kind && let PatKind::Binding(_, local_id, ..) = pat.kind
{ {
self.identifiers.insert(local_id); self.identifiers.insert(local_id);
@ -166,7 +166,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReferenceVisitor<'a, 'tcx> {
&& let ExprKind::Path(ref path) = expr.kind && let ExprKind::Path(ref path) = expr.kind
&& let QPath::Resolved(_, path) = path && let QPath::Resolved(_, path) = path
&& let Res::Local(local_id) = path.res && let Res::Local(local_id) = path.res
&& let Some(Node::Pat(pat)) = self.cx.tcx.hir().find(local_id) && let Some(Node::Pat(pat)) = self.cx.tcx.opt_hir_node(local_id)
&& let PatKind::Binding(_, local_id, ..) = pat.kind && let PatKind::Binding(_, local_id, ..) = pat.kind
&& self.identifiers.contains(&local_id) && self.identifiers.contains(&local_id)
{ {

View file

@ -91,7 +91,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
let node = if hir_id.local_id == ItemLocalId::from_u32(0) { let node = if hir_id.local_id == ItemLocalId::from_u32(0) {
// In this case, we can just use `find`, `Owner`'s `node` field is private anyway so we can't // In this case, we can just use `find`, `Owner`'s `node` field is private anyway so we can't
// reimplement it even if we wanted to // reimplement it even if we wanted to
cx.tcx.hir().find(hir_id) cx.tcx.opt_hir_node(hir_id)
} else { } else {
let Some(owner) = cx.tcx.hir_owner_nodes(hir_id.owner).as_owner() else { let Some(owner) = cx.tcx.hir_owner_nodes(hir_id.owner).as_owner() else {
return; return;

View file

@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
{ {
let parent = cx.tcx.hir().get_parent_item(hir_id).def_id; let parent = cx.tcx.hir().get_parent_item(hir_id).def_id;
if parent != CRATE_DEF_ID { if parent != CRATE_DEF_ID {
if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent) { if let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(parent) {
if let hir::ItemKind::Trait(..) = &item.kind { if let hir::ItemKind::Trait(..) = &item.kind {
return; return;
} }

View file

@ -220,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingFieldsInDebug {
&& 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.hir().find_by_def_id(self_def_id) && let Some(Node::Item(self_item)) = cx.tcx.opt_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

@ -213,7 +213,7 @@ fn check_for_unsequenced_reads(vis: &mut ReadVisitor<'_, '_>) {
if parent_id == cur_id { if parent_id == cur_id {
break; break;
} }
let Some(parent_node) = map.find(parent_id) else { break }; let Some(parent_node) = vis.cx.tcx.opt_hir_node(parent_id) else { break };
let stop_early = match parent_node { let stop_early = match parent_node {
Node::Expr(expr) => check_expr(vis, expr), Node::Expr(expr) => check_expr(vis, expr),

View file

@ -113,8 +113,9 @@ 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) = hir if let Some(body) = cx
.find_by_def_id(closure) .tcx
.opt_hir_node_by_def_id(closure)
.and_then(associated_body) .and_then(associated_body)
.map(|(_, body_id)| hir.body(body_id)) .map(|(_, body_id)| hir.body(body_id))
{ {
@ -412,7 +413,7 @@ impl<'tcx> euv::Delegate<'tcx> for MutablyUsedVariablesCtxt<'tcx> {
], ],
), ),
.. ..
}) = self.tcx.hir().get(cmt.hir_id) }) = self.tcx.hir_node(cmt.hir_id)
{ {
self.async_closures.insert(*def_id); self.async_closures.insert(*def_id);
} }
@ -521,7 +522,7 @@ impl<'tcx> Visitor<'tcx> for FnNeedsMutVisitor<'_, 'tcx> {
let Self { cx, used_fn_def_ids } = self; let Self { cx, used_fn_def_ids } = self;
// #11182; do not lint if mutability is required elsewhere // #11182; do not lint if mutability is required elsewhere
if let Node::Expr(expr) = cx.tcx.hir().get(hir_id) if let Node::Expr(expr) = cx.tcx.hir_node(hir_id)
&& let Some(parent) = get_parent_node(cx.tcx, expr.hir_id) && let Some(parent) = get_parent_node(cx.tcx, expr.hir_id)
&& let ty::FnDef(def_id, _) = cx && let ty::FnDef(def_id, _) = cx
.tcx .tcx

View file

@ -454,7 +454,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
if parent_id == cur_expr.hir_id { if parent_id == cur_expr.hir_id {
break; break;
} }
if let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find(parent_id) { if let Some(Node::Expr(parent_expr)) = cx.tcx.opt_hir_node(parent_id) {
match &parent_expr.kind { match &parent_expr.kind {
ExprKind::AddrOf(..) => { ExprKind::AddrOf(..) => {
// `&e` => `e` must be referenced. // `&e` => `e` must be referenced.

View file

@ -94,7 +94,6 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
{ {
let mut non_send_fields = Vec::new(); let mut non_send_fields = Vec::new();
let hir_map = cx.tcx.hir();
for variant in adt_def.variants() { for variant in adt_def.variants() {
for field in &variant.fields { for field in &variant.fields {
if let Some(field_hir_id) = field if let Some(field_hir_id) = field
@ -104,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
&& !is_lint_allowed(cx, NON_SEND_FIELDS_IN_SEND_TY, field_hir_id) && !is_lint_allowed(cx, NON_SEND_FIELDS_IN_SEND_TY, field_hir_id)
&& let field_ty = field.ty(cx.tcx, impl_trait_args) && let field_ty = field.ty(cx.tcx, impl_trait_args)
&& !ty_allowed_in_send(cx, field_ty, send_trait) && !ty_allowed_in_send(cx, field_ty, send_trait)
&& let Node::Field(field_def) = hir_map.get(field_hir_id) && let Node::Field(field_def) = cx.tcx.hir_node(field_hir_id)
{ {
non_send_fields.push(NonSendField { non_send_fields.push(NonSendField {
def: field_def, def: field_def,

View file

@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
&& last_segment.ident == ident && last_segment.ident == ident
// resolve the path to its defining binding pattern // resolve the path to its defining binding pattern
&& let Res::Local(binding_id) = cx.qpath_res(&qpath, expr.hir_id) && let Res::Local(binding_id) = cx.qpath_res(&qpath, expr.hir_id)
&& let Node::Pat(binding_pat) = cx.tcx.hir().get(binding_id) && let Node::Pat(binding_pat) = cx.tcx.hir_node(binding_id)
// the previous binding has the same mutability // the previous binding has the same mutability
&& find_binding(binding_pat, ident).is_some_and(|bind| bind.1 == mutability) && find_binding(binding_pat, ident).is_some_and(|bind| bind.1 == mutability)
// the local does not change the effect of assignments to the binding. see #11290 // the local does not change the effect of assignments to the binding. see #11290

View file

@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
Some(trait_ref) => { Some(trait_ref) => {
let mut methods_in_trait: BTreeSet<Symbol> = if let Some(Node::TraitRef(TraitRef { let mut methods_in_trait: BTreeSet<Symbol> = if let Some(Node::TraitRef(TraitRef {
path, .. path, ..
})) = cx.tcx.hir().find(trait_ref.hir_ref_id) })) = cx.tcx.opt_hir_node(trait_ref.hir_ref_id)
&& let Res::Def(DefKind::Trait, did) = path.res && let Res::Def(DefKind::Trait, did) = path.res
{ {
// FIXME: if // FIXME: if

View file

@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
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 self_id = cx.tcx.local_def_id_to_hir_id(self_local_did)
&& let Some(Node::Item(x)) = cx.tcx.hir().find(self_id) && let Some(Node::Item(x)) = cx.tcx.opt_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

@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
// Check for more than one binary operation in the implemented function // Check for more than one binary operation in the implemented function
// Linting when multiple operations are involved can result in false positives // Linting when multiple operations are involved can result in false positives
&& let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id && let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id
&& let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get_by_def_id(parent_fn) && let hir::Node::ImplItem(impl_item) = cx.tcx.hir_node_by_def_id(parent_fn)
&& let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind && let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind
&& let body = cx.tcx.hir().body(body_id) && let body = cx.tcx.hir().body(body_id)
&& let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id && let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id

View file

@ -321,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
_: Span, _: Span,
def_id: LocalDefId, def_id: LocalDefId,
) { ) {
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id( let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.opt_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))
@ -368,8 +368,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
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 Some(hir::Node::Item(item)) = cx
.tcx .tcx
.hir() .opt_hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
.find_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

@ -82,7 +82,7 @@ impl LateLintPass<'_> for UnnecessaryStruct {
fn is_mutable(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn is_mutable(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let Some(hir_id) = path_to_local(expr) if let Some(hir_id) = path_to_local(expr)
&& let Node::Pat(pat) = cx.tcx.hir().get(hir_id) && let Node::Pat(pat) = cx.tcx.hir_node(hir_id)
{ {
matches!(pat.kind, PatKind::Binding(BindingAnnotation::MUT, ..)) matches!(pat.kind, PatKind::Binding(BindingAnnotation::MUT, ..))
} else { } else {

View file

@ -281,7 +281,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
} }
if let Some(id) = path_to_local(recv) if let Some(id) = path_to_local(recv)
&& let Node::Pat(pat) = cx.tcx.hir().get(id) && let Node::Pat(pat) = cx.tcx.hir_node(id)
&& let PatKind::Binding(ann, ..) = pat.kind && let PatKind::Binding(ann, ..) = pat.kind
&& ann != BindingAnnotation::MUT && ann != BindingAnnotation::MUT
{ {

View file

@ -218,7 +218,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Ve
ExprKind::Path(ref qpath) => match cx.qpath_res(qpath, expr.hir_id) { ExprKind::Path(ref qpath) => match cx.qpath_res(qpath, expr.hir_id) {
Res::Local(hir_id) => { Res::Local(hir_id) => {
let parent_id = cx.tcx.hir().parent_id(hir_id); let parent_id = cx.tcx.hir().parent_id(hir_id);
if let Some(Node::Local(Local { init: Some(init), .. })) = cx.tcx.hir().find(parent_id) { if let Some(Node::Local(Local { init: Some(init), .. })) = cx.tcx.hir_node(parent_id) {
path_to_matched_type(cx, init) path_to_matched_type(cx, init)
} else { } else {
None None

View file

@ -74,7 +74,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
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.hir().find_by_def_id(second_parent_id) { if let Some(Node::Item(item)) = cx.tcx.opt_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

@ -269,7 +269,7 @@ fn fn_kind_pat(tcx: TyCtxt<'_>, kind: &FnKind<'_>, body: &Body<'_>, hir_id: HirI
FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")), FnKind::Method(.., sig) => (fn_header_search_pat(sig.header), Pat::Str("")),
FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, body.value).1), FnKind::Closure => return (Pat::Str(""), expr_search_pat(tcx, body.value).1),
}; };
let start_pat = match tcx.hir().get(hir_id) { let start_pat = match tcx.hir_node(hir_id) {
Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => { Node::Item(Item { vis_span, .. }) | Node::ImplItem(ImplItem { vis_span, .. }) => {
if vis_span.is_empty() { if vis_span.is_empty() {
start_pat start_pat

View file

@ -531,7 +531,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
kind: ExprKind::Lit(_), kind: ExprKind::Lit(_),
span, span,
.. ..
}) = self.lcx.tcx.hir().get(body_id.hir_id) }) = self.lcx.tcx.hir_node(body_id.hir_id)
&& is_direct_expn_of(*span, "cfg").is_some() && is_direct_expn_of(*span, "cfg").is_some()
{ {
return None; return None;

View file

@ -176,10 +176,10 @@ pub fn expr_or_init<'a, 'b, 'tcx: 'b>(cx: &LateContext<'tcx>, mut expr: &'a Expr
/// canonical binding `HirId`. /// canonical binding `HirId`.
pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> { pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
let hir = cx.tcx.hir(); let hir = cx.tcx.hir();
if let Some(Node::Pat(pat)) = hir.find(hir_id) if let Some(Node::Pat(pat)) = cx.tcx.opt_hir_node(hir_id)
&& matches!(pat.kind, PatKind::Binding(BindingAnnotation::NONE, ..)) && matches!(pat.kind, PatKind::Binding(BindingAnnotation::NONE, ..))
&& let parent = hir.parent_id(hir_id) && let parent = hir.parent_id(hir_id)
&& let Some(Node::Local(local)) = hir.find(parent) && let Some(Node::Local(local)) = cx.tcx.opt_hir_node(parent)
{ {
return local.init; return local.init;
} }
@ -563,7 +563,7 @@ 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 hir.find_by_def_id(local_id) { let item_kind = match tcx.opt_hir_node_by_def_id(local_id) {
Some(Node::Crate(r#mod)) => { Some(Node::Crate(r#mod)) => {
root_mod = ItemKind::Mod(r#mod); root_mod = ItemKind::Mod(r#mod);
&root_mod &root_mod
@ -712,7 +712,7 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, def_id: LocalDefId) ->
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id); let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
let parent_impl = cx.tcx.hir().get_parent_item(hir_id); let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
if parent_impl != hir::CRATE_OWNER_ID if parent_impl != hir::CRATE_OWNER_ID
&& let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent_impl.def_id) && let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_impl.def_id)
&& let hir::ItemKind::Impl(impl_) = &item.kind && let hir::ItemKind::Impl(impl_) = &item.kind
{ {
return impl_.of_trait.as_ref(); return impl_.of_trait.as_ref();
@ -1242,7 +1242,7 @@ 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.hir().find_by_def_id(parent_id) { match cx.tcx.opt_hir_node_by_def_id(parent_id) {
Some( Some(
Node::Item(Item { ident, .. }) Node::Item(Item { ident, .. })
| Node::TraitItem(TraitItem { ident, .. }) | Node::TraitItem(TraitItem { ident, .. })
@ -1319,7 +1319,7 @@ pub fn get_enclosing_block<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Optio
let map = &cx.tcx.hir(); let map = &cx.tcx.hir();
let enclosing_node = map let enclosing_node = map
.get_enclosing_scope(hir_id) .get_enclosing_scope(hir_id)
.and_then(|enclosing_id| map.find(enclosing_id)); .and_then(|enclosing_id| cx.tcx.opt_hir_node(enclosing_id));
enclosing_node.and_then(|node| match node { enclosing_node.and_then(|node| match node {
Node::Block(block) => Some(block), Node::Block(block) => Some(block),
Node::Item(&Item { Node::Item(&Item {
@ -2691,7 +2691,7 @@ impl<'tcx> ExprUseNode<'tcx> {
if let Some(Node::Expr(Expr { if let Some(Node::Expr(Expr {
kind: ExprKind::Closure(c), kind: ExprKind::Closure(c),
.. ..
})) = cx.tcx.hir().find(hir_id) })) = cx.tcx.opt_hir_node(hir_id)
{ {
match c.fn_decl.output { match c.fn_decl.output {
FnRetTy::DefaultReturn(_) => None, FnRetTy::DefaultReturn(_) => None,
@ -2757,7 +2757,7 @@ pub fn expr_use_ctxt<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Optio
walk_to_expr_usage(cx, e, &mut |parent, child_id| { walk_to_expr_usage(cx, e, &mut |parent, child_id| {
// LocalTableInContext returns the wrong lifetime, so go use `expr_adjustments` instead. // LocalTableInContext returns the wrong lifetime, so go use `expr_adjustments` instead.
if adjustments.is_empty() if adjustments.is_empty()
&& let Node::Expr(e) = cx.tcx.hir().get(child_id) && let Node::Expr(e) = cx.tcx.hir_node(child_id)
{ {
adjustments = cx.typeck_results().expr_adjustments(e); adjustments = cx.typeck_results().expr_adjustments(e);
} }