1
Fork 0

get_parent and find_parent

This commit is contained in:
Michael Goulet 2023-01-03 17:30:35 +00:00
parent 6af339dbfa
commit b1b19bd851
29 changed files with 88 additions and 95 deletions

View file

@ -3460,7 +3460,7 @@ impl<'hir> Node<'hir> {
/// ```ignore (illustrative) /// ```ignore (illustrative)
/// ctor /// ctor
/// .ctor_hir_id() /// .ctor_hir_id()
/// .and_then(|ctor_id| tcx.hir().find(tcx.hir().parent_id(ctor_id))) /// .and_then(|ctor_id| tcx.hir().find_parent(ctor_id))
/// .and_then(|parent| parent.ident()) /// .and_then(|parent| parent.ident())
/// ``` /// ```
pub fn ident(&self) -> Option<Ident> { pub fn ident(&self) -> Option<Ident> {

View file

@ -2936,7 +2936,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) = let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
hir.get(fn_hir_id) else { return None }; hir.get(fn_hir_id) else { return None };
let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(i), .. }) = let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(i), .. }) =
hir.get(hir.parent_id(fn_hir_id)) else { bug!("ImplItem should have Impl parent") }; hir.get_parent(fn_hir_id) else { bug!("ImplItem should have Impl parent") };
let trait_ref = self.instantiate_mono_trait_ref( let trait_ref = self.instantiate_mono_trait_ref(
i.of_trait.as_ref()?, i.of_trait.as_ref()?,

View file

@ -1108,7 +1108,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => { ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
// Do not try to infer the return type for a impl method coming from a trait // Do not try to infer the return type for a impl method coming from a trait
if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) = if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) =
tcx.hir().get(tcx.hir().parent_id(hir_id)) tcx.hir().get_parent(hir_id)
&& i.of_trait.is_some() && i.of_trait.is_some()
{ {
<dyn AstConv<'_>>::ty_of_fn( <dyn AstConv<'_>>::ty_of_fn(

View file

@ -103,7 +103,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
// `min_const_generics`. // `min_const_generics`.
Some(parent_def_id.to_def_id()) Some(parent_def_id.to_def_id())
} else { } else {
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id)); let parent_node = tcx.hir().get_parent(hir_id);
match parent_node { match parent_node {
// HACK(eddyb) this provides the correct generics for repeat // HACK(eddyb) this provides the correct generics for repeat
// expressions' count (i.e. `N` in `[x; N]`), and explicit // expressions' count (i.e. `N` in `[x; N]`), and explicit
@ -320,7 +320,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
// provide junk type parameter defs for const blocks. // provide junk type parameter defs for const blocks.
if let Node::AnonConst(_) = node { if let Node::AnonConst(_) = node {
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id)); let parent_node = tcx.hir().get_parent(hir_id);
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node { if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
params.push(ty::GenericParamDef { params.push(ty::GenericParamDef {
index: next_index(), index: next_index(),

View file

@ -402,7 +402,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
} }
Node::AnonConst(_) => { Node::AnonConst(_) => {
let parent_node = tcx.hir().get(tcx.hir().parent_id(hir_id)); let parent_node = tcx.hir().get_parent(hir_id);
match parent_node { match parent_node {
Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. }) Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) | Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
@ -445,7 +445,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
.. ..
}, },
) if let Node::TraitRef(trait_ref) = ) if let Node::TraitRef(trait_ref) =
tcx.hir().get(tcx.hir().parent_id(binding_id)) tcx.hir().get_parent(binding_id)
&& e.hir_id == hir_id => && e.hir_id == hir_id =>
{ {
let Some(trait_def_id) = trait_ref.trait_def_id() else { let Some(trait_def_id) = trait_ref.trait_def_id() else {
@ -472,7 +472,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
Node::TypeBinding( Node::TypeBinding(
binding @ &TypeBinding { hir_id: binding_id, gen_args, ref kind, .. }, binding @ &TypeBinding { hir_id: binding_id, gen_args, ref kind, .. },
) if let Node::TraitRef(trait_ref) = ) if let Node::TraitRef(trait_ref) =
tcx.hir().get(tcx.hir().parent_id(binding_id)) tcx.hir().get_parent(binding_id)
&& let Some((idx, _)) = && let Some((idx, _)) =
gen_args.args.iter().enumerate().find(|(_, arg)| { gen_args.args.iter().enumerate().find(|(_, arg)| {
if let GenericArg::Const(ct) = arg { if let GenericArg::Const(ct) = arg {

View file

@ -294,10 +294,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
if let hir::Node::Block(block) = node { if let hir::Node::Block(block) = node {
// check that the body's parent is an fn // check that the body's parent is an fn
let parent = self let parent = self.tcx.hir().get_parent(self.tcx.hir().parent_id(block.hir_id));
.tcx
.hir()
.get(self.tcx.hir().parent_id(self.tcx.hir().parent_id(block.hir_id)));
if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })) = if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })) =
(&block.expr, parent) (&block.expr, parent)
{ {

View file

@ -258,10 +258,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Path { res: hir::def::Res::Local(hir_id), .. }, hir::Path { res: hir::def::Res::Local(hir_id), .. },
)) => { )) => {
if let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(*hir_id) { if let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(*hir_id) {
let parent = self.tcx.hir().parent_id(pat.hir_id);
primary_span = pat.span; primary_span = pat.span;
secondary_span = pat.span; secondary_span = pat.span;
match self.tcx.hir().find(parent) { match self.tcx.hir().find_parent(pat.hir_id) {
Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => { Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => {
primary_span = ty.span; primary_span = ty.span;
post_message = " type"; post_message = " type";
@ -857,7 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => None, _ => None,
}?; }?;
match hir.find(hir.parent_id(expr.hir_id))? { match hir.find_parent(expr.hir_id)? {
Node::ExprField(field) => { Node::ExprField(field) => {
if field.ident.name == local.name && field.is_shorthand { if field.ident.name == local.name && field.is_shorthand {
return Some(local.name); return Some(local.name);
@ -1040,7 +1039,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(hir::Node::Expr(hir::Expr { if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..), kind: hir::ExprKind::Assign(..),
.. ..
})) = self.tcx.hir().find(self.tcx.hir().parent_id(expr.hir_id)) })) = self.tcx.hir().find_parent(expr.hir_id)
{ {
if mutability.is_mut() { if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign` // Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
@ -1267,9 +1266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut sugg = vec![]; let mut sugg = vec![];
if let Some(hir::Node::ExprField(field)) = if let Some(hir::Node::ExprField(field)) = self.tcx.hir().find_parent(expr.hir_id) {
self.tcx.hir().find(self.tcx.hir().parent_id(expr.hir_id))
{
// `expr` is a literal field for a struct, only suggest if appropriate // `expr` is a literal field for a struct, only suggest if appropriate
if field.is_shorthand { if field.is_shorthand {
// This is a field literal // This is a field literal

View file

@ -1083,7 +1083,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Do not suggest `if let x = y` as `==` is way more likely to be the intention. // Do not suggest `if let x = y` as `==` is way more likely to be the intention.
let hir = self.tcx.hir(); let hir = self.tcx.hir();
if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) = if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
hir.get(hir.parent_id(hir.parent_id(expr.hir_id))) hir.get_parent(hir.parent_id(expr.hir_id))
{ {
err.span_suggestion_verbose( err.span_suggestion_verbose(
expr.span.shrink_to_lo(), expr.span.shrink_to_lo(),
@ -2462,7 +2462,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_label(field.span, "method, not a field"); err.span_label(field.span, "method, not a field");
let expr_is_call = let expr_is_call =
if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) = if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
self.tcx.hir().get(self.tcx.hir().parent_id(expr.hir_id)) self.tcx.hir().get_parent(expr.hir_id)
{ {
expr.hir_id == callee.hir_id expr.hir_id == callee.hir_id
} else { } else {

View file

@ -1435,9 +1435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool { pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool {
let mut contained_in_place = false; let mut contained_in_place = false;
while let hir::Node::Expr(parent_expr) = while let hir::Node::Expr(parent_expr) = self.tcx.hir().get_parent(expr_id) {
self.tcx.hir().get(self.tcx.hir().parent_id(expr_id))
{
match &parent_expr.kind { match &parent_expr.kind {
hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => { hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
if lhs.hir_id == expr_id { if lhs.hir_id == expr_id {

View file

@ -1803,7 +1803,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir_id: call_hir_id, hir_id: call_hir_id,
span: call_span, span: call_span,
.. ..
}) = hir.get(hir.parent_id(expr.hir_id)) }) = hir.get_parent(expr.hir_id)
&& callee.hir_id == expr.hir_id && callee.hir_id == expr.hir_id
{ {
if self.closure_span_overlaps_error(error, *call_span) { if self.closure_span_overlaps_error(error, *call_span) {

View file

@ -1451,7 +1451,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let filename = tcx.sess.source_map().span_to_filename(span); let filename = tcx.sess.source_map().span_to_filename(span);
let parent_node = let parent_node =
self.tcx.hir().get(self.tcx.hir().parent_id(hir_id)); self.tcx.hir().get_parent(hir_id);
let msg = format!( let msg = format!(
"you must specify a type for this binding, like `{}`", "you must specify a type for this binding, like `{}`",
concrete_type, concrete_type,

View file

@ -936,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
res.descr(), res.descr(),
), ),
); );
match self.tcx.hir().get(self.tcx.hir().parent_id(pat.hir_id)) { match self.tcx.hir().get_parent(pat.hir_id) {
hir::Node::PatField(..) => { hir::Node::PatField(..) => {
e.span_suggestion_verbose( e.span_suggestion_verbose(
ident.span.shrink_to_hi(), ident.span.shrink_to_hi(),

View file

@ -585,17 +585,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let hir::StmtKind::Local(local) = &stmt.kind else { continue; }; let hir::StmtKind::Local(local) = &stmt.kind else { continue; };
local.pat.walk(&mut find_compatible_candidates); local.pat.walk(&mut find_compatible_candidates);
} }
match hir.find(hir.parent_id(blk.hir_id)) { match hir.find_parent(blk.hir_id) {
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => { Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => match hir.find_parent(*hir_id) {
match hir.find(hir.parent_id(*hir_id)) {
Some(hir::Node::Arm(hir::Arm { pat, .. })) => { Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
pat.walk(&mut find_compatible_candidates); pat.walk(&mut find_compatible_candidates);
} }
Some( Some(
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. }) hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
| hir::Node::ImplItem(hir::ImplItem { | hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Fn(_, body), kind: hir::ImplItemKind::Fn(_, body), ..
..
}) })
| hir::Node::TraitItem(hir::TraitItem { | hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)), kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
@ -622,8 +620,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let_.pat.walk(&mut find_compatible_candidates); let_.pat.walk(&mut find_compatible_candidates);
} }
_ => {} _ => {}
} },
}
_ => {} _ => {}
} }

View file

@ -1526,7 +1526,7 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) { fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
let map = cx.tcx.hir(); let map = cx.tcx.hir();
if matches!(map.get(map.parent_id(field.hir_id)), Node::Variant(_)) { if matches!(map.get_parent(field.hir_id), Node::Variant(_)) {
return; return;
} }
self.perform_lint(cx, "field", field.def_id, field.vis_span, false); self.perform_lint(cx, "field", field.def_id, field.vis_span, false);

View file

@ -143,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
TyKind::Path(QPath::Resolved(_, path)) => { TyKind::Path(QPath::Resolved(_, path)) => {
if lint_ty_kind_usage(cx, &path.res) { if lint_ty_kind_usage(cx, &path.res) {
let hir = cx.tcx.hir(); let hir = cx.tcx.hir();
let span = match hir.find(hir.parent_id(ty.hir_id)) { let span = match hir.find_parent(ty.hir_id) {
Some(Node::Pat(Pat { Some(Node::Pat(Pat {
kind: kind:
PatKind::Path(qpath) PatKind::Path(qpath)

View file

@ -444,7 +444,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) { fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
if let PatKind::Binding(_, hid, ident, _) = p.kind { if let PatKind::Binding(_, hid, ident, _) = p.kind {
if let hir::Node::PatField(field) = cx.tcx.hir().get(cx.tcx.hir().parent_id(hid)) { if let hir::Node::PatField(field) = cx.tcx.hir().get_parent(hid) {
if !field.is_shorthand { if !field.is_shorthand {
// Only check if a new name has been introduced, to avoid warning // Only check if a new name has been introduced, to avoid warning
// on both the struct definition and this pattern. // on both the struct definition and this pattern.

View file

@ -129,8 +129,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
// which are represented as `ExprKind::Struct`. // which are represented as `ExprKind::Struct`.
let par_id = cx.tcx.hir().parent_id(expr.hir_id); let par_id = cx.tcx.hir().parent_id(expr.hir_id);
let Node::ExprField(field) = cx.tcx.hir().get(par_id) else { return false }; let Node::ExprField(field) = cx.tcx.hir().get(par_id) else { return false };
let field_par_id = cx.tcx.hir().parent_id(field.hir_id); let Node::Expr(struct_expr) = cx.tcx.hir().get_parent(field.hir_id) else { return false };
let Node::Expr(struct_expr) = cx.tcx.hir().get(field_par_id) else { return false };
if !is_range_literal(struct_expr) { if !is_range_literal(struct_expr) {
return false; return false;
}; };

View file

@ -246,7 +246,7 @@ impl<'hir> Map<'hir> {
}, },
Node::Variant(_) => DefKind::Variant, Node::Variant(_) => DefKind::Variant,
Node::Ctor(variant_data) => { Node::Ctor(variant_data) => {
let ctor_of = match self.find(self.parent_id(hir_id)) { let ctor_of = match self.find_parent(hir_id) {
Some(Node::Item(..)) => def::CtorOf::Struct, Some(Node::Item(..)) => def::CtorOf::Struct,
Some(Node::Variant(..)) => def::CtorOf::Variant, Some(Node::Variant(..)) => def::CtorOf::Variant,
_ => unreachable!(), _ => unreachable!(),
@ -257,7 +257,7 @@ impl<'hir> Map<'hir> {
} }
} }
Node::AnonConst(_) => { Node::AnonConst(_) => {
let inline = match self.find(self.parent_id(hir_id)) { let inline = match self.find_parent(hir_id) {
Some(Node::Expr(&Expr { Some(Node::Expr(&Expr {
kind: ExprKind::ConstBlock(ref anon_const), .. kind: ExprKind::ConstBlock(ref anon_const), ..
})) if anon_const.hir_id == hir_id => true, })) if anon_const.hir_id == hir_id => true,
@ -317,6 +317,14 @@ impl<'hir> Map<'hir> {
.unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id))) .unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id)))
} }
pub fn get_parent(self, hir_id: HirId) -> Node<'hir> {
self.get(self.parent_id(hir_id))
}
pub fn find_parent(self, hir_id: HirId) -> Option<Node<'hir>> {
self.find(self.opt_parent_id(hir_id)?)
}
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found. /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
pub fn find(self, id: HirId) -> Option<Node<'hir>> { pub fn find(self, id: HirId) -> Option<Node<'hir>> {
if id.local_id == ItemLocalId::from_u32(0) { if id.local_id == ItemLocalId::from_u32(0) {
@ -664,7 +672,7 @@ impl<'hir> Map<'hir> {
/// Checks if the node is left-hand side of an assignment. /// Checks if the node is left-hand side of an assignment.
pub fn is_lhs(self, id: HirId) -> bool { pub fn is_lhs(self, id: HirId) -> bool {
match self.find(self.parent_id(id)) { match self.find_parent(id) {
Some(Node::Expr(expr)) => match expr.kind { Some(Node::Expr(expr)) => match expr.kind {
ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id, ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
_ => false, _ => false,
@ -892,7 +900,7 @@ impl<'hir> Map<'hir> {
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident), Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
// A `Ctor` doesn't have an identifier itself, but its parent // A `Ctor` doesn't have an identifier itself, but its parent
// struct/variant does. Compare with `hir::Map::opt_span`. // struct/variant does. Compare with `hir::Map::opt_span`.
Node::Ctor(..) => match self.find(self.parent_id(id))? { Node::Ctor(..) => match self.find_parent(id)? {
Node::Item(item) => Some(item.ident), Node::Item(item) => Some(item.ident),
Node::Variant(variant) => Some(variant.ident), Node::Variant(variant) => Some(variant.ident),
_ => unreachable!(), _ => unreachable!(),
@ -1093,7 +1101,7 @@ impl<'hir> Map<'hir> {
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when /// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
/// called with the HirId for the `{ ... }` anon const /// called with the HirId for the `{ ... }` anon const
pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> { pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
match self.get(self.parent_id(anon_const)) { match self.get_parent(anon_const) {
Node::GenericParam(GenericParam { Node::GenericParam(GenericParam {
def_id: param_id, def_id: param_id,
kind: GenericParamKind::Const { .. }, kind: GenericParamKind::Const { .. },

View file

@ -457,7 +457,7 @@ impl<'tcx> TyCtxt<'tcx> {
.def_id .def_id
.as_local() .as_local()
.map(|id| hir.local_def_id_to_hir_id(id)) .map(|id| hir.local_def_id_to_hir_id(id))
.and_then(|id| self.hir().find(self.hir().parent_id(id))) .and_then(|id| self.hir().find_parent(id))
.as_ref() .as_ref()
.and_then(|node| node.generics()) .and_then(|node| node.generics())
{ {

View file

@ -838,8 +838,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(def_id.as_local()?); let hir_id = hir.local_def_id_to_hir_id(def_id.as_local()?);
let parent_node = hir.parent_id(hir_id); match hir.find_parent(hir_id) {
match hir.find(parent_node) {
Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => { Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => {
get_name(err, &local.pat.kind) get_name(err, &local.pat.kind)
} }
@ -3287,8 +3286,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path && let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
&& let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id) && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
&& let parent_hir_id = self.tcx.hir().parent_id(binding.hir_id) && let Some(parent) = self.tcx.hir().find_parent(binding.hir_id)
&& let Some(parent) = self.tcx.hir().find(parent_hir_id)
{ {
// We've reached the root of the method call chain... // We've reached the root of the method call chain...
if let hir::Node::Local(local) = parent if let hir::Node::Local(local) = parent

View file

@ -131,7 +131,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
_ => return false, _ => return false,
} }
matches!(map.find(map.parent_id(id)), Some(Node::Param(_))) matches!(map.find_parent(id), Some(Node::Param(_)))
} }
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
@ -157,7 +157,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
if is_argument(*map, cmt.hir_id) { if is_argument(*map, 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(map.parent_id(parent_id)) { if let Some(Node::Expr(..)) = map.find_parent(parent_id) {
return; return;
} }

View file

@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
&& 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.hir().find(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(cx.tcx.hir().parent_id(hir_id)) { match cx.tcx.hir().find_parent(hir_id) {
Some(Node::Param(..)) => (), Some(Node::Param(..)) => (),
Some(Node::Local(local)) => { Some(Node::Local(local)) => {
let Some(ty) = local.ty else { return }; let Some(ty) = local.ty else { return };

View file

@ -140,8 +140,8 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
fn opt_parent_assign_span<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<AssignmentExpr> { fn opt_parent_assign_span<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<AssignmentExpr> {
let map = &cx.tcx.hir(); let map = &cx.tcx.hir();
if let Some(Node::Expr(parent_arm_expr)) = map.find(map.parent_id(ex.hir_id)) { if let Some(Node::Expr(parent_arm_expr)) = map.find_parent(ex.hir_id) {
return match map.find(map.parent_id(parent_arm_expr.hir_id)) { return match map.find_parent(parent_arm_expr.hir_id) {
Some(Node::Local(parent_let_expr)) => Some(AssignmentExpr::Local { Some(Node::Local(parent_let_expr)) => Some(AssignmentExpr::Local {
span: parent_let_expr.span, span: parent_let_expr.span,
pat_span: parent_let_expr.pat.span(), pat_span: parent_let_expr.pat.span(),
@ -183,8 +183,7 @@ fn sugg_with_curlies<'a>(
// If the parent is already an arm, and the body is another match statement, // If the parent is already an arm, and the body is another match statement,
// we need curly braces around suggestion // we need curly braces around suggestion
let parent_node_id = cx.tcx.hir().parent_id(match_expr.hir_id); if let Node::Arm(arm) = &cx.tcx.hir().get_parent(match_expr.hir_id) {
if let Node::Arm(arm) = &cx.tcx.hir().get(parent_node_id) {
if let ExprKind::Match(..) = arm.body.kind { if let ExprKind::Match(..) = arm.body.kind {
cbrace_end = format!("\n{indent}}}"); cbrace_end = format!("\n{indent}}}");
// Fix body indent due to the match // Fix body indent due to the match

View file

@ -100,7 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
} }
// Exclude non-inherent impls // Exclude non-inherent impls
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().parent_id(hir_id)) { if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) {
if matches!( if matches!(
item.kind, item.kind,
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..) ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)

View file

@ -299,7 +299,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
} }
// Exclude non-inherent impls // Exclude non-inherent impls
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().parent_id(hir_id)) { if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) {
if matches!( if matches!(
item.kind, item.kind,
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..) ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)

View file

@ -21,7 +21,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
return; return;
} }
let map = &cx.tcx.hir(); let map = &cx.tcx.hir();
let opt_parent_node = map.find(map.parent_id(expr.hir_id)); let opt_parent_node = map.find_parent(expr.hir_id);
if_chain! { if_chain! {
if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node; if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
if is_questionmark_desugar_marked_call(parent_expr); if is_questionmark_desugar_marked_call(parent_expr);
@ -192,7 +192,7 @@ fn fmt_stmts_and_call(
let mut stmts_and_call_snippet = stmts_and_call.join(&format!("{}{}", ";\n", " ".repeat(call_expr_indent))); let mut stmts_and_call_snippet = stmts_and_call.join(&format!("{}{}", ";\n", " ".repeat(call_expr_indent)));
// expr is not in a block statement or result expression position, wrap in a block // expr is not in a block statement or result expression position, wrap in a block
let parent_node = cx.tcx.hir().find(cx.tcx.hir().parent_id(call_expr.hir_id)); let parent_node = cx.tcx.hir().find_parent(call_expr.hir_id);
if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) { if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) {
let block_indent = call_expr_indent + 4; let block_indent = call_expr_indent + 4;
stmts_and_call_snippet = stmts_and_call_snippet =

View file

@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
} }
// Abort if the method is implementing a trait or of it a trait method. // Abort if the method is implementing a trait or of it a trait method.
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().parent_id(hir_id)) { if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) {
if matches!( if matches!(
item.kind, item.kind,
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..) ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)

View file

@ -1058,7 +1058,7 @@ fn get_parent_local<'hir>(cx: &LateContext<'hir>, expr: &'hir hir::Expr<'hir>) -
fn get_parent_local_hir_id<'hir>(cx: &LateContext<'hir>, hir_id: hir::HirId) -> Option<&'hir hir::Local<'hir>> { fn get_parent_local_hir_id<'hir>(cx: &LateContext<'hir>, hir_id: hir::HirId) -> Option<&'hir hir::Local<'hir>> {
let map = cx.tcx.hir(); let map = cx.tcx.hir();
match map.find(map.parent_id(hir_id)) { match map.find_parent((hir_id)) {
Some(hir::Node::Local(local)) => Some(local), Some(hir::Node::Local(local)) => Some(local),
Some(hir::Node::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id), Some(hir::Node::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id),
_ => None, _ => None,

View file

@ -1287,7 +1287,7 @@ pub fn contains_return(expr: &hir::Expr<'_>) -> bool {
/// Gets the parent node, if any. /// Gets the parent node, if any.
pub fn get_parent_node(tcx: TyCtxt<'_>, id: HirId) -> Option<Node<'_>> { pub fn get_parent_node(tcx: TyCtxt<'_>, id: HirId) -> Option<Node<'_>> {
tcx.hir().parent_iter(id).next().map(|(_, node)| node) tcx.hir().find_parent(id)
} }
/// Gets the parent expression, if any - this is useful to constrain a lint. /// Gets the parent expression, if any - this is useful to constrain a lint.
@ -2075,7 +2075,7 @@ pub fn is_no_core_crate(cx: &LateContext<'_>) -> bool {
/// } /// }
/// ``` /// ```
pub fn is_trait_impl_item(cx: &LateContext<'_>, hir_id: HirId) -> bool { pub fn is_trait_impl_item(cx: &LateContext<'_>, hir_id: HirId) -> bool {
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().parent_id(hir_id)) { if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) {
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
} else { } else {
false false