mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
get_parent and find_parent
This commit is contained in:
parent
bd1d8971cf
commit
70f6c478f6
9 changed files with 14 additions and 15 deletions
|
@ -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 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
|||
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 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
|
|||
&& 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 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
|
|||
}
|
||||
|
||||
// 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 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
|
|||
}
|
||||
|
||||
// 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 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
|
|||
}
|
||||
|
||||
// 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 a new issue