Auto merge of #120951 - matthiaskrgr:rollup-0nnm7dv, r=matthiaskrgr

Rollup of 8 pull requests

Successful merges:

 - #110483 (Create try_new function for ThinBox)
 - #120740 (Make cmath.rs a single file)
 - #120872 (hir: Refactor getters for HIR parents)
 - #120880 (add note on comparing vtables / function pointers)
 - #120885 (interpret/visitor: ensure we only see normalized types)
 - #120888 (assert_unsafe_precondition cleanup)
 - #120897 (Encode `coroutine_for_closure` for foreign crates)
 - #120937 ([docs] Update armv6k-nintendo-3ds platform docs for outdated info)

r? `@ghost`
`@rustbot` modify labels: rollup
This commit is contained in:
bors 2024-02-12 00:34:22 +00:00
commit 036d00bff6
39 changed files with 78 additions and 102 deletions

View File

@ -46,11 +46,10 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
return;
};
if let ConstantSource::Constant = source
&& let Some(node) = cx.tcx.hir().find_parent(e.hir_id)
&& let Node::Item(Item {
kind: ItemKind::Const(..),
..
}) = node
}) = cx.tcx.parent_hir_node(e.hir_id)
{
return;
}

View File

@ -67,25 +67,20 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv
}
fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let map = cx.tcx.hir();
if let Some(parent_id) = map.opt_parent_id(expr.hir_id) {
let parent = cx.tcx.hir_node(parent_id);
let expr = match parent {
Node::Block(block) => {
if let Some(parent_expr) = block.expr {
parent_expr
} else {
return false;
}
},
Node::Expr(expr) => expr,
_ => return false,
};
let parent = cx.tcx.parent_hir_node(expr.hir_id);
let expr = match parent {
Node::Block(block) => {
if let Some(parent_expr) = block.expr {
parent_expr
} else {
return false;
}
},
Node::Expr(expr) => expr,
_ => return false,
};
matches!(expr.kind, ExprKind::Cast(..))
} else {
false
}
matches!(expr.kind, ExprKind::Cast(..))
}
/// Returns the type T of the pointed to *const [T] or *mut [T] and the mutability of the slice if

View File

@ -65,7 +65,7 @@ pub(super) fn check<'tcx>(
&& let ExprKind::Path(qpath) = inner.kind
&& let QPath::Resolved(None, Path { res, .. }) = qpath
&& let Res::Local(hir_id) = res
&& let parent = cx.tcx.hir().get_parent(*hir_id)
&& let parent = cx.tcx.parent_hir_node(*hir_id)
&& let Node::Local(local) = parent
{
if let Some(ty) = local.ty

View File

@ -63,7 +63,7 @@ impl LateLintPass<'_> for DbgMacro {
ExprKind::Block(..) => {
// If the `dbg!` macro is a "free" statement and not contained within other expressions,
// remove the whole statement.
if let Some(Node::Stmt(_)) = cx.tcx.hir().find_parent(expr.hir_id)
if let Node::Stmt(_) = cx.tcx.parent_hir_node(expr.hir_id)
&& let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span)
{
(macro_call.span.to(semi_span), String::new())

View File

@ -128,8 +128,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
},
_,
) => {
if let Some(parent) = self.cx.tcx.hir().find_parent(expr.hir_id)
&& let Some(fn_sig) = parent.fn_sig()
if let Some(fn_sig) = self.cx.tcx.parent_hir_node(expr.hir_id).fn_sig()
&& let FnRetTy::Return(_ty) = fn_sig.decl.output
{
// We cannot check the exact type since it's a `hir::Ty`` which does not implement `is_numeric`

View File

@ -1088,7 +1088,7 @@ fn report<'tcx>(
//
// e.g. `&mut x.y.z` where `x` is a union, and accessing `z` requires a
// deref through `ManuallyDrop<_>` will not compile.
let parent_id = cx.tcx.hir().parent_id(expr.hir_id);
let parent_id = cx.tcx.parent_hir_id(expr.hir_id);
if parent_id == data.first_expr.hir_id {
return;
}

View File

@ -131,7 +131,7 @@ fn is_argument(tcx: TyCtxt<'_>, id: HirId) -> bool {
_ => return false,
}
matches!(tcx.hir().find_parent(id), Some(Node::Param(_)))
matches!(tcx.parent_hir_node(id), Node::Param(_))
}
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
@ -156,8 +156,8 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
let map = &self.cx.tcx.hir();
if is_argument(self.cx.tcx, cmt.hir_id) {
// Skip closure arguments
let parent_id = map.parent_id(cmt.hir_id);
if let Some(Node::Expr(..)) = map.find_parent(parent_id) {
let parent_id = self.cx.tcx.parent_hir_id(cmt.hir_id);
if let Node::Expr(..) = self.cx.tcx.parent_hir_node(parent_id) {
return;
}

View File

@ -53,7 +53,7 @@ pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body:
pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
if let ImplItemKind::Fn(_, body_id) = impl_item.kind
&& let hir::Node::Item(item) = cx.tcx.hir().get_parent(impl_item.hir_id())
&& let hir::Node::Item(item) = cx.tcx.parent_hir_node(impl_item.hir_id())
&& let hir::ItemKind::Impl(impl_) = item.kind
&& let hir::Impl { of_trait, .. } = *impl_
&& of_trait.is_none()
@ -72,7 +72,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
pub(super) fn check_trait_item(cx: &LateContext<'_>, trait_item: &TraitItem<'_>, avoid_breaking_exported_api: bool) {
if !avoid_breaking_exported_api
&& let TraitItemKind::Fn(_, _) = trait_item.kind
&& let hir::Node::Item(item) = cx.tcx.hir().get_parent(trait_item.hir_id())
&& let hir::Node::Item(item) = cx.tcx.parent_hir_node(trait_item.hir_id())
// ^^ (Will always be a trait)
&& !item.vis_span.is_empty() // Is public
&& !is_in_test_function(cx.tcx, trait_item.hir_id())

View File

@ -41,8 +41,8 @@ impl<'tcx> LateLintPass<'tcx> for IgnoredUnitPatterns {
return;
}
match cx.tcx.hir().get_parent(pat.hir_id) {
Node::Param(param) if matches!(cx.tcx.hir().get_parent(param.hir_id), Node::Item(_)) => {
match cx.tcx.parent_hir_node(pat.hir_id) {
Node::Param(param) if matches!(cx.tcx.parent_hir_node(param.hir_id), Node::Item(_)) => {
// Ignore function parameters
return;
},

View File

@ -242,12 +242,8 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
} = *self;
if let Some(use_info) = slice_lint_info.get_mut(&local_id)
// Check if this is even a local we're interested in
&& let map = cx.tcx.hir()
// Checking for slice indexing
&& let parent_id = map.parent_id(expr.hir_id)
&& let parent_id = cx.tcx.parent_hir_id(expr.hir_id)
&& let hir::Node::Expr(parent_expr) = cx.tcx.hir_node(parent_id)
&& let hir::ExprKind::Index(_, index_expr, _) = parent_expr.kind
&& let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr)
@ -255,11 +251,10 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
&& index_value < max_suggested_slice
// Make sure that this slice index is read only
&& let maybe_addrof_id = map.parent_id(parent_id)
&& let hir::Node::Expr(maybe_addrof_expr) = cx.tcx.hir_node(maybe_addrof_id)
&& let hir::Node::Expr(maybe_addrof_expr) = cx.tcx.parent_hir_node(parent_id)
&& 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, cx.tcx.hir().span(parent_expr.hir_id)));
return;
}

View File

@ -269,7 +269,7 @@ impl {self_ty_without_ref} {{
// }
let span_behind_impl = cx
.tcx
.def_span(cx.tcx.hir().parent_id(item.hir_id()).owner.def_id)
.def_span(cx.tcx.parent_hir_id(item.hir_id()).owner.def_id)
.shrink_to_lo();
let sugg = format!(

View File

@ -285,7 +285,7 @@ fn elision_suggestions(
.iter()
.filter(|usage| named_lifetime(usage).map_or(false, |id| elidable_lts.contains(&id)))
.map(|usage| {
match cx.tcx.hir().get_parent(usage.hir_id) {
match cx.tcx.parent_hir_node(usage.hir_id) {
Node::Ty(Ty {
kind: TyKind::Ref(..), ..
}) => {

View File

@ -62,8 +62,7 @@ pub(super) fn check<'tcx>(
if let Node::Pat(pat) = node
&& let PatKind::Binding(bind_ann, ..) = pat.kind
&& !matches!(bind_ann, BindingAnnotation(_, Mutability::Mut))
&& let parent_node = cx.tcx.hir().parent_id(hir_id)
&& let Node::Local(parent_let_expr) = cx.tcx.hir_node(parent_node)
&& let Node::Local(parent_let_expr) = cx.tcx.parent_hir_node(hir_id)
&& let Some(init) = parent_let_expr.init
{
match init.kind {

View File

@ -68,8 +68,8 @@ impl LateLintPass<'_> for ManualHashOne {
&& let ExprKind::MethodCall(seg, build_hasher, [], _) = init.kind
&& seg.ident.name == sym!(build_hasher)
&& let Node::Stmt(local_stmt) = cx.tcx.hir().get_parent(local.hir_id)
&& let Node::Block(block) = cx.tcx.hir().get_parent(local_stmt.hir_id)
&& let Node::Stmt(local_stmt) = cx.tcx.parent_hir_node(local.hir_id)
&& let Node::Block(block) = cx.tcx.parent_hir_node(local_stmt.hir_id)
&& let mut stmts = block.stmts.iter()
.skip_while(|stmt| stmt.hir_id != local_stmt.hir_id)
@ -91,7 +91,7 @@ impl LateLintPass<'_> for ManualHashOne {
// `hasher.finish()`, may be anywhere in a statement or the trailing expr of the block
&& let Some(path_expr) = local_used_once(cx, (maybe_finish_stmt, block.expr), hasher)
&& let Node::Expr(finish_expr) = cx.tcx.hir().get_parent(path_expr.hir_id)
&& let Node::Expr(finish_expr) = cx.tcx.parent_hir_node(path_expr.hir_id)
&& !finish_expr.span.from_expansion()
&& let ExprKind::MethodCall(seg, _, [], _) = finish_expr.kind
&& seg.ident.name == sym!(finish)

View File

@ -79,9 +79,9 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
&& let Node::Pat(_) = cx.tcx.hir_node(hir_id)
{
// Apply only to params or locals with annotated types
match cx.tcx.hir().find_parent(hir_id) {
Some(Node::Param(..)) => (),
Some(Node::Local(local)) => {
match cx.tcx.parent_hir_node(hir_id) {
Node::Param(..) => (),
Node::Local(local) => {
let Some(ty) = local.ty else { return };
if matches!(ty.kind, TyKind::Infer) {
return;

View File

@ -36,7 +36,7 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
.to_string();
// Do we need to add ';' to suggestion ?
if let Node::Stmt(stmt) = cx.tcx.hir().get_parent(expr.hir_id)
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)
&& let StmtKind::Expr(_) = stmt.kind
&& match match_body.kind {
// We don't need to add a ; to blocks, unless that block is from a macro expansion
@ -146,18 +146,16 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
/// Returns true if the `ex` match expression is in a local (`let`) or assign expression
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_parent(ex.hir_id) {
return match map.find_parent(parent_arm_expr.hir_id) {
Some(Node::Local(parent_let_expr)) => Some(AssignmentExpr::Local {
if let Node::Expr(parent_arm_expr) = cx.tcx.parent_hir_node(ex.hir_id) {
return match cx.tcx.parent_hir_node(parent_arm_expr.hir_id) {
Node::Local(parent_let_expr) => Some(AssignmentExpr::Local {
span: parent_let_expr.span,
pat_span: parent_let_expr.pat.span(),
}),
Some(Node::Expr(Expr {
Node::Expr(Expr {
kind: ExprKind::Assign(parent_assign_expr, match_expr, _),
..
})) => Some(AssignmentExpr::Assign {
}) => Some(AssignmentExpr::Assign {
span: parent_assign_expr.span,
match_span: match_expr.span,
}),
@ -191,7 +189,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
if let Node::Arm(arm) = &cx.tcx.hir().get_parent(match_expr.hir_id) {
if let Node::Arm(arm) = &cx.tcx.parent_hir_node(match_expr.hir_id) {
if let ExprKind::Match(..) = arm.body.kind {
cbrace_end = format!("\n{indent}}}");
// Fix body indent due to the match

View File

@ -199,7 +199,7 @@ fn get_pat_binding<'tcx>(
return span.map(|span| PatBindingInfo {
span,
byref_ident,
is_field: matches!(cx.tcx.hir().get_parent(local), Node::PatField(_)),
is_field: matches!(cx.tcx.parent_hir_node(local), Node::PatField(_)),
});
}
}

View File

@ -4458,7 +4458,7 @@ impl Methods {
_ => {},
},
("drain", ..) => {
if let Node::Stmt(Stmt { hir_id: _, kind, .. }) = cx.tcx.hir().get_parent(expr.hir_id)
if let Node::Stmt(Stmt { hir_id: _, kind, .. }) = cx.tcx.parent_hir_node(expr.hir_id)
&& matches!(kind, StmtKind::Semi(_))
&& args.len() <= 1
{

View File

@ -21,9 +21,9 @@ fn is_unwrap_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, receiver: &Expr<'_>) {
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(receiver).peel_refs(), sym::RwLock)
&& let Node::Expr(unwrap_call_expr) = cx.tcx.hir().get_parent(expr.hir_id)
&& let Node::Expr(unwrap_call_expr) = cx.tcx.parent_hir_node(expr.hir_id)
&& is_unwrap_call(cx, unwrap_call_expr)
&& let parent = cx.tcx.hir().get_parent(unwrap_call_expr.hir_id)
&& let parent = cx.tcx.parent_hir_node(unwrap_call_expr.hir_id)
&& let Node::Local(local) = parent
&& let Some(mir) = enclosing_mir(cx.tcx, expr.hir_id)
&& let Some((local, _)) = mir

View File

@ -16,7 +16,7 @@ use super::UNNECESSARY_FOLD;
/// Changing `fold` to `sum` needs it sometimes when the return type can't be
/// inferred. This checks for some common cases where it can be safely omitted
fn needs_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
let parent = cx.tcx.hir().get_parent(expr.hir_id);
let parent = cx.tcx.parent_hir_node(expr.hir_id);
// some common cases where turbofish isn't needed:
// - assigned to a local variable with a type annotation

View File

@ -76,7 +76,7 @@ pub(super) fn check(
(expr.span.with_lo(call_args[0].span.hi()), String::new()),
];
// try to also remove the unsafe block if present
if let hir::Node::Block(block) = cx.tcx.hir().get_parent(expr.hir_id)
if let hir::Node::Block(block) = cx.tcx.parent_hir_node(expr.hir_id)
&& let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules
{
suggs.extend([

View File

@ -206,10 +206,9 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
///
/// When such a read is found, the lint is triggered.
fn check_for_unsequenced_reads(vis: &mut ReadVisitor<'_, '_>) {
let map = &vis.cx.tcx.hir();
let mut cur_id = vis.write_expr.hir_id;
loop {
let parent_id = map.parent_id(cur_id);
let parent_id = vis.cx.tcx.parent_hir_id(cur_id);
if parent_id == cur_id {
break;
}

View File

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

View File

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

View File

@ -449,7 +449,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
let mut dereferenced_expr = expr;
let mut needs_check_adjustment = true;
loop {
let parent_id = cx.tcx.hir().parent_id(cur_expr.hir_id);
let parent_id = cx.tcx.parent_hir_id(cur_expr.hir_id);
if parent_id == cur_expr.hir_id {
break;
}

View File

@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
}
fn used_in_comparison_with_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let Some(Node::Expr(parent_expr)) = cx.tcx.hir().find_parent(expr.hir_id) else {
let Node::Expr(parent_expr) = cx.tcx.parent_hir_node(expr.hir_id) else {
return false;
};
let ExprKind::Binary(op, lhs, rhs) = parent_expr.kind else {

View File

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

View File

@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
_ => return false,
};
if let Node::Expr(parent) = cx.tcx.hir().get_parent(e.hir_id)
if let Node::Expr(parent) = cx.tcx.parent_hir_node(e.hir_id)
&& parent.precedence().order() > ExprPrecedence::Cast.order()
{
sugg = format!("({sugg})");

View File

@ -153,13 +153,10 @@ fn all_bindings_are_for_conv<'tcx>(
let Some(locals) = locals.iter().map(|e| path_to_local(e)).collect::<Option<Vec<_>>>() else {
return false;
};
let Some(local_parents) = locals
let local_parents = locals
.iter()
.map(|&l| cx.tcx.hir().find_parent(l))
.collect::<Option<Vec<_>>>()
else {
return false;
};
.map(|l| cx.tcx.parent_hir_node(*l))
.collect::<Vec<_>>();
local_parents
.iter()

View File

@ -19,9 +19,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if is_questionmark_desugar_marked_call(expr) {
return;
}
let map = &cx.tcx.hir();
let opt_parent_node = map.find_parent(expr.hir_id);
if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node
if let hir::Node::Expr(parent_expr) = cx.tcx.parent_hir_node(expr.hir_id)
&& is_questionmark_desugar_marked_call(parent_expr)
{
return;
@ -183,8 +181,8 @@ 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_parent(call_expr.hir_id);
if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) {
let parent_node = cx.tcx.parent_hir_node(call_expr.hir_id);
if !matches!(parent_node, Node::Block(_)) && !matches!(parent_node, Node::Stmt(_)) {
let block_indent = call_expr_indent + 4;
stmts_and_call_snippet =
reindent_multiline(stmts_and_call_snippet.into(), true, Some(block_indent)).into_owned();

View File

@ -116,7 +116,7 @@ impl LateLintPass<'_> for UnnecessaryBoxReturns {
fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) {
// Ignore implementations of traits, because the lint should be on the
// trait, not on the implementation of it.
let Node::Item(parent) = cx.tcx.hir().get_parent(item.hir_id()) else {
let Node::Item(parent) = cx.tcx.parent_hir_node(item.hir_id()) else {
return;
};
let ItemKind::Impl(parent) = parent.kind else { return };

View File

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

View File

@ -156,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
&& let Some(local_def_id) = def_id.as_local()
&& cx.tcx.def_kind(def_id) == DefKind::Fn
&& cx.tcx.asyncness(def_id).is_async()
&& !is_node_func_call(cx.tcx.hir().get_parent(hir_id), path.span)
&& !is_node_func_call(cx.tcx.parent_hir_node(hir_id), path.span)
{
self.async_fns_as_value.insert(local_def_id);
}

View File

@ -208,7 +208,7 @@ struct MutationVisitor<'tcx> {
/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
/// expression: that will be where the actual method call is.
fn is_option_as_mut_use(tcx: TyCtxt<'_>, expr_id: HirId) -> bool {
if let Node::Expr(mutating_expr) = tcx.hir().get_parent(expr_id)
if let Node::Expr(mutating_expr) = tcx.parent_hir_node(expr_id)
&& let ExprKind::MethodCall(path, ..) = mutating_expr.kind
{
path.ident.name.as_str() == "as_mut"

View File

@ -1007,9 +1007,9 @@ 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_parent(hir_id) {
Some(hir::Node::Local(local)) => Some(local),
Some(hir::Node::Pat(pattern)) => get_parent_local_hir_id(cx, pattern.hir_id),
match cx.tcx.parent_hir_node(hir_id) {
hir::Node::Local(local) => Some(local),
hir::Node::Pat(pattern) => get_parent_local_hir_id(cx, pattern.hir_id),
_ => None,
}
}

View File

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

View File

@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
if let Some(vec_args) = higher::VecArgs::hir(cx, expr.peel_borrows()) {
// search for `let foo = vec![_]` expressions where all uses of `foo`
// adjust to slices or call a method that exist on slices (e.g. len)
if let Node::Local(local) = cx.tcx.hir().get_parent(expr.hir_id)
if let Node::Local(local) = cx.tcx.parent_hir_node(expr.hir_id)
// for now ignore locals with type annotations.
// this is to avoid compile errors when doing the suggestion here: let _: Vec<_> = vec![..];
&& local.ty.is_none()
@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
}
// if the local pattern has a specified type, do not lint.
else if let Some(_) = higher::VecArgs::hir(cx, expr)
&& let Node::Local(local) = cx.tcx.hir().get_parent(expr.hir_id)
&& let Node::Local(local) = cx.tcx.parent_hir_node(expr.hir_id)
&& local.ty.is_some()
{
let span = expr.span.ctxt().outer_expn_data().call_site;

View File

@ -182,11 +182,9 @@ pub fn expr_or_init<'a, 'b, 'tcx: 'b>(cx: &LateContext<'tcx>, mut expr: &'a Expr
/// Note: If you have an expression that references a binding `x`, use `path_to_local` to get the
/// canonical binding `HirId`.
pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
let hir = cx.tcx.hir();
if let Node::Pat(pat) = cx.tcx.hir_node(hir_id)
&& matches!(pat.kind, PatKind::Binding(BindingAnnotation::NONE, ..))
&& let parent = hir.parent_id(hir_id)
&& let Node::Local(local) = cx.tcx.hir_node(parent)
&& let Node::Local(local) = cx.tcx.parent_hir_node(hir_id)
{
return local.init;
}
@ -333,7 +331,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.
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)
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
&& let Node::Item(item) = cx.tcx.parent_hir_node(hir_id)
&& let ItemKind::Impl(imp) = item.kind
{
imp.of_trait.is_some()
@ -1311,7 +1309,7 @@ pub fn contains_return<'tcx>(expr: impl Visitable<'tcx>) -> bool {
/// Gets the parent node, if any.
pub fn get_parent_node(tcx: TyCtxt<'_>, id: HirId) -> Option<Node<'_>> {
tcx.hir().find_parent(id)
Some(tcx.parent_hir_node(id))
}
/// Gets the parent expression, if any - this is useful to constrain a lint.
@ -2227,7 +2225,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_parent(hir_id) {
if let Node::Item(item) = cx.tcx.parent_hir_node(hir_id) {
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
} else {
false

View File

@ -237,7 +237,7 @@ fn path_segment_certainty(
},
// `get_parent` because `hir_id` refers to a `Pat`, and we're interested in the node containing the `Pat`.
Res::Local(hir_id) => match cx.tcx.hir().get_parent(hir_id) {
Res::Local(hir_id) => match cx.tcx.parent_hir_node(hir_id) {
// An argument's type is always certain.
Node::Param(..) => Certainty::Certain(None),
// A local's type is certain if its type annotation is certain or it has an initializer whose