get_parent and find_parent
This commit is contained in:
parent
6af339dbfa
commit
b1b19bd851
@ -3460,7 +3460,7 @@ impl<'hir> Node<'hir> {
|
||||
/// ```ignore (illustrative)
|
||||
/// ctor
|
||||
/// .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())
|
||||
/// ```
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
|
@ -2936,7 +2936,7 @@ fn suggest_trait_fn_ty_for_impl_fn_infer(
|
||||
let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
|
||||
hir.get(fn_hir_id) else { return None };
|
||||
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(
|
||||
i.of_trait.as_ref()?,
|
||||
|
@ -1108,7 +1108,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
|
||||
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
|
||||
// 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), .. }) =
|
||||
tcx.hir().get(tcx.hir().parent_id(hir_id))
|
||||
tcx.hir().get_parent(hir_id)
|
||||
&& i.of_trait.is_some()
|
||||
{
|
||||
<dyn AstConv<'_>>::ty_of_fn(
|
||||
|
@ -103,7 +103,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
|
||||
// `min_const_generics`.
|
||||
Some(parent_def_id.to_def_id())
|
||||
} 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 {
|
||||
// HACK(eddyb) this provides the correct generics for repeat
|
||||
// expressions' count (i.e. `N` in `[x; N]`), and explicit
|
||||
@ -320,7 +320,7 @@ enum Defaults {
|
||||
|
||||
// provide junk type parameter defs for const blocks.
|
||||
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 {
|
||||
params.push(ty::GenericParamDef {
|
||||
index: next_index(),
|
||||
|
@ -402,7 +402,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
||||
}
|
||||
|
||||
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 {
|
||||
Node::Ty(&Ty { kind: TyKind::Array(_, 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) =
|
||||
tcx.hir().get(tcx.hir().parent_id(binding_id))
|
||||
tcx.hir().get_parent(binding_id)
|
||||
&& e.hir_id == hir_id =>
|
||||
{
|
||||
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(
|
||||
binding @ &TypeBinding { hir_id: binding_id, gen_args, ref kind, .. },
|
||||
) if let Node::TraitRef(trait_ref) =
|
||||
tcx.hir().get(tcx.hir().parent_id(binding_id))
|
||||
tcx.hir().get_parent(binding_id)
|
||||
&& let Some((idx, _)) =
|
||||
gen_args.args.iter().enumerate().find(|(_, arg)| {
|
||||
if let GenericArg::Const(ct) = arg {
|
||||
|
@ -294,10 +294,7 @@ fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, sp: Span) -> Option<(Spa
|
||||
};
|
||||
if let hir::Node::Block(block) = node {
|
||||
// check that the body's parent is an fn
|
||||
let parent = self
|
||||
.tcx
|
||||
.hir()
|
||||
.get(self.tcx.hir().parent_id(self.tcx.hir().parent_id(block.hir_id)));
|
||||
let parent = self.tcx.hir().get_parent(self.tcx.hir().parent_id(block.hir_id));
|
||||
if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })) =
|
||||
(&block.expr, parent)
|
||||
{
|
||||
|
@ -258,10 +258,9 @@ fn annotate_expected_due_to_let_ty(
|
||||
hir::Path { res: hir::def::Res::Local(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;
|
||||
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), .. })) => {
|
||||
primary_span = ty.span;
|
||||
post_message = " type";
|
||||
@ -857,7 +856,7 @@ pub(crate) fn maybe_get_struct_pattern_shorthand_field(
|
||||
_ => None,
|
||||
}?;
|
||||
|
||||
match hir.find(hir.parent_id(expr.hir_id))? {
|
||||
match hir.find_parent(expr.hir_id)? {
|
||||
Node::ExprField(field) => {
|
||||
if field.ident.name == local.name && field.is_shorthand {
|
||||
return Some(local.name);
|
||||
@ -1040,7 +1039,7 @@ pub fn check_ref(
|
||||
if let Some(hir::Node::Expr(hir::Expr {
|
||||
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() {
|
||||
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
|
||||
@ -1267,9 +1266,7 @@ pub fn check_for_cast(
|
||||
|
||||
let mut sugg = vec![];
|
||||
|
||||
if let Some(hir::Node::ExprField(field)) =
|
||||
self.tcx.hir().find(self.tcx.hir().parent_id(expr.hir_id))
|
||||
{
|
||||
if let Some(hir::Node::ExprField(field)) = self.tcx.hir().find_parent(expr.hir_id) {
|
||||
// `expr` is a literal field for a struct, only suggest if appropriate
|
||||
if field.is_shorthand {
|
||||
// This is a field literal
|
||||
|
@ -1083,7 +1083,7 @@ fn check_expr_assign(
|
||||
// Do not suggest `if let x = y` as `==` is way more likely to be the intention.
|
||||
let hir = self.tcx.hir();
|
||||
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(
|
||||
expr.span.shrink_to_lo(),
|
||||
@ -2462,7 +2462,7 @@ fn ban_take_value_of_method(&self, expr: &hir::Expr<'_>, expr_t: Ty<'tcx>, field
|
||||
err.span_label(field.span, "method, not a field");
|
||||
let expr_is_call =
|
||||
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
|
||||
} else {
|
||||
|
@ -1435,9 +1435,7 @@ pub(in super::super) fn probe_instantiate_query_response(
|
||||
pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool {
|
||||
let mut contained_in_place = false;
|
||||
|
||||
while let hir::Node::Expr(parent_expr) =
|
||||
self.tcx.hir().get(self.tcx.hir().parent_id(expr_id))
|
||||
{
|
||||
while let hir::Node::Expr(parent_expr) = self.tcx.hir().get_parent(expr_id) {
|
||||
match &parent_expr.kind {
|
||||
hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
|
||||
if lhs.hir_id == expr_id {
|
||||
|
@ -1803,7 +1803,7 @@ fn adjust_fulfillment_error_for_expr_obligation(
|
||||
hir_id: call_hir_id,
|
||||
span: call_span,
|
||||
..
|
||||
}) = hir.get(hir.parent_id(expr.hir_id))
|
||||
}) = hir.get_parent(expr.hir_id)
|
||||
&& callee.hir_id == expr.hir_id
|
||||
{
|
||||
if self.closure_span_overlaps_error(error, *call_span) {
|
||||
|
@ -1451,7 +1451,7 @@ fn suggest_constraining_numerical_ty(
|
||||
let filename = tcx.sess.source_map().span_to_filename(span);
|
||||
|
||||
let parent_node =
|
||||
self.tcx.hir().get(self.tcx.hir().parent_id(hir_id));
|
||||
self.tcx.hir().get_parent(hir_id);
|
||||
let msg = format!(
|
||||
"you must specify a type for this binding, like `{}`",
|
||||
concrete_type,
|
||||
|
@ -936,7 +936,7 @@ fn emit_bad_pat_path(
|
||||
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(..) => {
|
||||
e.span_suggestion_verbose(
|
||||
ident.span.shrink_to_hi(),
|
||||
|
@ -585,45 +585,42 @@ pub fn consider_returning_binding(
|
||||
let hir::StmtKind::Local(local) = &stmt.kind else { continue; };
|
||||
local.pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
match hir.find(hir.parent_id(blk.hir_id)) {
|
||||
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => {
|
||||
match hir.find(hir.parent_id(*hir_id)) {
|
||||
Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
|
||||
pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
Some(
|
||||
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
|
||||
| hir::Node::ImplItem(hir::ImplItem {
|
||||
kind: hir::ImplItemKind::Fn(_, body),
|
||||
..
|
||||
})
|
||||
| hir::Node::TraitItem(hir::TraitItem {
|
||||
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
|
||||
..
|
||||
})
|
||||
| hir::Node::Expr(hir::Expr {
|
||||
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
|
||||
..
|
||||
}),
|
||||
) => {
|
||||
for param in hir.body(*body).params {
|
||||
param.pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
}
|
||||
Some(hir::Node::Expr(hir::Expr {
|
||||
kind:
|
||||
hir::ExprKind::If(
|
||||
hir::Expr { kind: hir::ExprKind::Let(let_), .. },
|
||||
then_block,
|
||||
_,
|
||||
),
|
||||
..
|
||||
})) if then_block.hir_id == *hir_id => {
|
||||
let_.pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
_ => {}
|
||||
match hir.find_parent(blk.hir_id) {
|
||||
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => match hir.find_parent(*hir_id) {
|
||||
Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
|
||||
pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
}
|
||||
Some(
|
||||
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
|
||||
| hir::Node::ImplItem(hir::ImplItem {
|
||||
kind: hir::ImplItemKind::Fn(_, body), ..
|
||||
})
|
||||
| hir::Node::TraitItem(hir::TraitItem {
|
||||
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
|
||||
..
|
||||
})
|
||||
| hir::Node::Expr(hir::Expr {
|
||||
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
|
||||
..
|
||||
}),
|
||||
) => {
|
||||
for param in hir.body(*body).params {
|
||||
param.pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
}
|
||||
Some(hir::Node::Expr(hir::Expr {
|
||||
kind:
|
||||
hir::ExprKind::If(
|
||||
hir::Expr { kind: hir::ExprKind::Let(let_), .. },
|
||||
then_block,
|
||||
_,
|
||||
),
|
||||
..
|
||||
})) if then_block.hir_id == *hir_id => {
|
||||
let_.pat.walk(&mut find_compatible_candidates);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
@ -1526,7 +1526,7 @@ fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::Forei
|
||||
|
||||
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
|
||||
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;
|
||||
}
|
||||
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);
|
||||
|
@ -143,7 +143,7 @@ fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) {
|
||||
TyKind::Path(QPath::Resolved(_, path)) => {
|
||||
if lint_ty_kind_usage(cx, &path.res) {
|
||||
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 {
|
||||
kind:
|
||||
PatKind::Path(qpath)
|
||||
|
@ -444,7 +444,7 @@ fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &hir::TraitItem<'_>)
|
||||
|
||||
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
|
||||
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 {
|
||||
// Only check if a new name has been introduced, to avoid warning
|
||||
// on both the struct definition and this pattern.
|
||||
|
@ -129,8 +129,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
|
||||
// which are represented as `ExprKind::Struct`.
|
||||
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 field_par_id = cx.tcx.hir().parent_id(field.hir_id);
|
||||
let Node::Expr(struct_expr) = cx.tcx.hir().get(field_par_id) else { return false };
|
||||
let Node::Expr(struct_expr) = cx.tcx.hir().get_parent(field.hir_id) else { return false };
|
||||
if !is_range_literal(struct_expr) {
|
||||
return false;
|
||||
};
|
||||
|
@ -246,7 +246,7 @@ pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
|
||||
},
|
||||
Node::Variant(_) => DefKind::Variant,
|
||||
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::Variant(..)) => def::CtorOf::Variant,
|
||||
_ => unreachable!(),
|
||||
@ -257,7 +257,7 @@ pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
|
||||
}
|
||||
}
|
||||
Node::AnonConst(_) => {
|
||||
let inline = match self.find(self.parent_id(hir_id)) {
|
||||
let inline = match self.find_parent(hir_id) {
|
||||
Some(Node::Expr(&Expr {
|
||||
kind: ExprKind::ConstBlock(ref anon_const), ..
|
||||
})) if anon_const.hir_id == hir_id => true,
|
||||
@ -317,6 +317,14 @@ pub fn parent_id(self, hir_id: HirId) -> HirId {
|
||||
.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.
|
||||
pub fn find(self, id: HirId) -> Option<Node<'hir>> {
|
||||
if id.local_id == ItemLocalId::from_u32(0) {
|
||||
@ -664,7 +672,7 @@ pub fn parent_owner_iter(self, current_id: HirId) -> ParentOwnerIterator<'hir> {
|
||||
|
||||
/// Checks if the node is left-hand side of an assignment.
|
||||
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 {
|
||||
ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
|
||||
_ => false,
|
||||
@ -892,7 +900,7 @@ fn opt_ident(self, id: HirId) -> Option<Ident> {
|
||||
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
|
||||
// A `Ctor` doesn't have an identifier itself, but its parent
|
||||
// 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::Variant(variant) => Some(variant.ident),
|
||||
_ => unreachable!(),
|
||||
@ -1093,7 +1101,7 @@ pub fn node_to_string(self, id: HirId) -> String {
|
||||
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
|
||||
/// called with the HirId for the `{ ... }` anon const
|
||||
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 {
|
||||
def_id: param_id,
|
||||
kind: GenericParamKind::Const { .. },
|
||||
|
@ -457,7 +457,7 @@ pub fn note_and_explain_type_err(
|
||||
.def_id
|
||||
.as_local()
|
||||
.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()
|
||||
.and_then(|node| node.generics())
|
||||
{
|
||||
|
@ -838,8 +838,7 @@ fn get_closure_name(&self, def_id: DefId, err: &mut Diagnostic, msg: &str) -> Op
|
||||
|
||||
let hir = self.tcx.hir();
|
||||
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_node) {
|
||||
match hir.find_parent(hir_id) {
|
||||
Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => {
|
||||
get_name(err, &local.pat.kind)
|
||||
}
|
||||
@ -3287,8 +3286,7 @@ fn point_at_chain(
|
||||
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
|
||||
&& let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
|
||||
&& 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_hir_id)
|
||||
&& let Some(parent) = self.tcx.hir().find_parent(binding.hir_id)
|
||||
{
|
||||
// We've reached the root of the method call chain...
|
||||
if let hir::Node::Local(local) = parent
|
||||
|
@ -131,7 +131,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
|
||||
_ => 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> {
|
||||
@ -157,7 +157,7 @@ fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
|
||||
if is_argument(*map, cmt.hir_id) {
|
||||
// Skip closure arguments
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
&& let Some(hir_id) = path_to_local(expr3)
|
||||
&& let Some(Node::Pat(_)) = cx.tcx.hir().find(hir_id) {
|
||||
// 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::Local(local)) => {
|
||||
let Some(ty) = local.ty else { return };
|
||||
|
@ -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> {
|
||||
let map = &cx.tcx.hir();
|
||||
|
||||
if let Some(Node::Expr(parent_arm_expr)) = map.find(map.parent_id(ex.hir_id)) {
|
||||
return match map.find(map.parent_id(parent_arm_expr.hir_id)) {
|
||||
if let Some(Node::Expr(parent_arm_expr)) = map.find_parent(ex.hir_id) {
|
||||
return match map.find_parent(parent_arm_expr.hir_id) {
|
||||
Some(Node::Local(parent_let_expr)) => Some(AssignmentExpr::Local {
|
||||
span: parent_let_expr.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,
|
||||
// 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_node_id) {
|
||||
if let Node::Arm(arm) = &cx.tcx.hir().get_parent(match_expr.hir_id) {
|
||||
if let ExprKind::Match(..) = arm.body.kind {
|
||||
cbrace_end = format!("\n{indent}}}");
|
||||
// Fix body indent due to the match
|
||||
|
@ -100,7 +100,7 @@ fn check_fn(
|
||||
}
|
||||
|
||||
// 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!(
|
||||
item.kind,
|
||||
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
|
||||
|
@ -299,7 +299,7 @@ fn check_fn(
|
||||
}
|
||||
|
||||
// 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!(
|
||||
item.kind,
|
||||
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
|
||||
|
@ -21,7 +21,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
return;
|
||||
}
|
||||
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 let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
|
||||
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)));
|
||||
// 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(_))) {
|
||||
let block_indent = call_expr_indent + 4;
|
||||
stmts_and_call_snippet =
|
||||
|
@ -91,7 +91,7 @@ fn check_fn(
|
||||
}
|
||||
|
||||
// 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!(
|
||||
item.kind,
|
||||
ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
|
||||
|
@ -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>> {
|
||||
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::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id),
|
||||
_ => None,
|
||||
|
@ -1287,7 +1287,7 @@ pub fn contains_return(expr: &hir::Expr<'_>) -> bool {
|
||||
|
||||
/// Gets the parent node, if any.
|
||||
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.
|
||||
@ -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 {
|
||||
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(_), .. }))
|
||||
} else {
|
||||
false
|
||||
|
Loading…
Reference in New Issue
Block a user