Remove clippy_utils::get_parent_node

This commit is contained in:
Alex Macleod 2024-02-25 16:32:54 +00:00
parent c469cb0023
commit bee4111a61
20 changed files with 47 additions and 58 deletions

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::macros::macro_backtrace; use clippy_utils::macros::macro_backtrace;
use clippy_utils::ty::expr_sig; use clippy_utils::ty::expr_sig;
use clippy_utils::{get_parent_node, is_default_equivalent, path_def_id}; use clippy_utils::{is_default_equivalent, path_def_id};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::intravisit::{walk_ty, Visitor}; use rustc_hir::intravisit::{walk_ty, Visitor};
@ -100,7 +100,7 @@ impl<'tcx> Visitor<'tcx> for InferVisitor {
} }
fn given_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn given_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match get_parent_node(cx.tcx, expr.hir_id) { match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Local(Local { ty: Some(ty), .. }) => { Node::Local(Local { ty: Some(ty), .. }) => {
let mut v = InferVisitor::default(); let mut v = InferVisitor::default();
v.visit_ty(ty); v.visit_ty(ty);

View File

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::NumericLiteral; use clippy_utils::numeric_literal::NumericLiteral;
use clippy_utils::source::snippet_opt; use clippy_utils::source::snippet_opt;
use clippy_utils::visitors::{for_each_expr, Visitable}; use clippy_utils::visitors::{for_each_expr, Visitable};
use clippy_utils::{get_parent_expr, get_parent_node, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local}; use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
use rustc_ast::{LitFloatType, LitIntType, LitKind}; use rustc_ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
@ -264,7 +264,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
} }
// Local usage // Local usage
} else if let Res::Local(hir_id) = res } else if let Res::Local(hir_id) = res
&& let Node::Local(l) = get_parent_node(cx.tcx, hir_id) && let Node::Local(l) = cx.tcx.parent_hir_node(hir_id)
{ {
if let Some(e) = l.init if let Some(e) = l.init
&& is_cast_from_ty_alias(cx, e, cast_from) && is_cast_from_ty_alias(cx, e, cast_from)

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
use clippy_utils::visitors::for_each_expr_with_closures; use clippy_utils::visitors::for_each_expr_with_closures;
use clippy_utils::{get_enclosing_block, get_parent_node, path_to_local_id}; use clippy_utils::{get_enclosing_block, path_to_local_id};
use core::ops::ControlFlow; use core::ops::ControlFlow;
use rustc_hir::{Block, ExprKind, HirId, LangItem, Local, Node, PatKind}; use rustc_hir::{Block, ExprKind, HirId, LangItem, Local, Node, PatKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -94,7 +94,7 @@ fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Bloc
// `id` appearing in the left-hand side of an assignment is not a read access: // `id` appearing in the left-hand side of an assignment is not a read access:
// //
// id = ...; // Not reading `id`. // id = ...; // Not reading `id`.
if let Node::Expr(parent) = get_parent_node(cx.tcx, expr.hir_id) if let Node::Expr(parent) = cx.tcx.parent_hir_node(expr.hir_id)
&& let ExprKind::Assign(lhs, ..) = parent.kind && let ExprKind::Assign(lhs, ..) = parent.kind
&& path_to_local_id(lhs, id) && path_to_local_id(lhs, id)
{ {
@ -108,7 +108,7 @@ fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Bloc
// Only assuming this for "official" methods defined on the type. For methods defined in extension // Only assuming this for "official" methods defined on the type. For methods defined in extension
// traits (identified as local, based on the orphan rule), pessimistically assume that they might // traits (identified as local, based on the orphan rule), pessimistically assume that they might
// have side effects, so consider them a read. // have side effects, so consider them a read.
if let Node::Expr(parent) = get_parent_node(cx.tcx, expr.hir_id) if let Node::Expr(parent) = cx.tcx.parent_hir_node(expr.hir_id)
&& let ExprKind::MethodCall(_, receiver, _, _) = parent.kind && let ExprKind::MethodCall(_, receiver, _, _) = parent.kind
&& path_to_local_id(receiver, id) && path_to_local_id(receiver, id)
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id) && let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
@ -117,7 +117,7 @@ fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Bloc
// The method call is a statement, so the return value is not used. That's not a read access: // The method call is a statement, so the return value is not used. That's not a read access:
// //
// id.foo(args); // id.foo(args);
if let Node::Stmt(..) = get_parent_node(cx.tcx, parent.hir_id) { if let Node::Stmt(..) = cx.tcx.parent_hir_node(parent.hir_id) {
return ControlFlow::Continue(()); return ControlFlow::Continue(());
} }

View File

@ -2,9 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren; use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::ty::{implements_trait, is_manually_drop, peel_mid_ty_refs}; use clippy_utils::ty::{implements_trait, is_manually_drop, peel_mid_ty_refs};
use clippy_utils::{ use clippy_utils::{expr_use_ctxt, get_parent_expr, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode};
expr_use_ctxt, get_parent_expr, get_parent_node, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode,
};
use core::mem; use core::mem;
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX}; use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
@ -1008,7 +1006,7 @@ fn report<'tcx>(
data.first_expr.span, data.first_expr.span,
state.msg, state.msg,
|diag| { |diag| {
let (precedence, calls_field) = match get_parent_node(cx.tcx, data.first_expr.hir_id) { let (precedence, calls_field) = match cx.tcx.parent_hir_node(data.first_expr.hir_id) {
Node::Expr(e) => match e.kind { Node::Expr(e) => match e.kind {
ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => (0, false), ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => (0, false),
ExprKind::Call(..) => (PREC_POSTFIX, matches!(expr.kind, ExprKind::Field(..))), ExprKind::Call(..) => (PREC_POSTFIX, matches!(expr.kind, ExprKind::Field(..))),

View File

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_note; use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::is_must_use_func_call;
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item}; use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
use clippy_utils::{get_parent_node, is_must_use_func_call};
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node}; use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass; use rustc_session::declare_lint_pass;
@ -144,7 +144,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
// } // }
fn is_single_call_in_arm<'tcx>(cx: &LateContext<'tcx>, arg: &'tcx Expr<'_>, drop_expr: &'tcx Expr<'_>) -> bool { fn is_single_call_in_arm<'tcx>(cx: &LateContext<'tcx>, arg: &'tcx Expr<'_>, drop_expr: &'tcx Expr<'_>) -> bool {
if matches!(arg.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)) { if matches!(arg.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)) {
if let Node::Arm(Arm { body, .. }) = get_parent_node(cx.tcx, drop_expr.hir_id) { if let Node::Arm(Arm { body, .. }) = cx.tcx.parent_hir_node(drop_expr.hir_id) {
return body.hir_id == drop_expr.hir_id; return body.hir_id == drop_expr.hir_id;
} }
} }

View File

@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint;
use clippy_utils::get_parent_node;
use clippy_utils::ty::implements_trait; use clippy_utils::ty::implements_trait;
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_hir::{FnSig, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind}; use rustc_hir::{FnSig, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind};
@ -56,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for IterNotReturningIterator {
let name = item.ident.name.as_str(); let name = item.ident.name.as_str();
if matches!(name, "iter" | "iter_mut") if matches!(name, "iter" | "iter_mut")
&& !matches!( && !matches!(
get_parent_node(cx.tcx, item.hir_id()), cx.tcx.parent_hir_node(item.hir_id()),
Node::Item(Item { kind: ItemKind::Impl(i), .. }) if i.of_trait.is_some() Node::Item(Item { kind: ItemKind::Impl(i), .. }) if i.of_trait.is_some()
) )
{ {

View File

@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts}; use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
use clippy_utils::{ use clippy_utils::{
eq_expr_value, get_parent_expr_for_hir, get_parent_node, higher, is_else_clause, is_res_lang_ctor, over, path_res, eq_expr_value, get_parent_expr_for_hir, higher, is_else_clause, is_res_lang_ctor, over, path_res,
peel_blocks_with_stmt, peel_blocks_with_stmt,
}; };
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -123,7 +123,7 @@ fn strip_return<'hir>(expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
/// Manually check for coercion casting by checking if the type of the match operand or let expr /// Manually check for coercion casting by checking if the type of the match operand or let expr
/// differs with the assigned local variable or the function return type. /// differs with the assigned local variable or the function return type.
fn expr_ty_matches_p_ty(cx: &LateContext<'_>, expr: &Expr<'_>, p_expr: &Expr<'_>) -> bool { fn expr_ty_matches_p_ty(cx: &LateContext<'_>, expr: &Expr<'_>, p_expr: &Expr<'_>) -> bool {
match get_parent_node(cx.tcx, p_expr.hir_id) { match cx.tcx.parent_hir_node(p_expr.hir_id) {
// Compare match_expr ty with local in `let local = match match_expr {..}` // Compare match_expr ty with local in `let local = match match_expr {..}`
Node::Local(local) => { Node::Local(local) => {
let results = cx.typeck_results(); let results = cx.typeck_results();

View File

@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::get_parent_node;
use clippy_utils::source::snippet_with_context; use clippy_utils::source::snippet_with_context;
use clippy_utils::ty::is_copy; use clippy_utils::ty::is_copy;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -48,7 +47,7 @@ pub(super) fn check(
} }
if is_copy(cx, ty) { if is_copy(cx, ty) {
let parent_is_suffix_expr = match get_parent_node(cx.tcx, expr.hir_id) { let parent_is_suffix_expr = match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Expr(parent) => match parent.kind { Node::Expr(parent) => match parent.kind {
// &*x is a nop, &x.clone() is not // &*x is a nop, &x.clone() is not
ExprKind::AddrOf(..) => return, ExprKind::AddrOf(..) => return,

View File

@ -4,8 +4,8 @@ use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::sugg::Sugg; use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, make_normalized_projection, make_projection}; use clippy_utils::ty::{is_type_diagnostic_item, make_normalized_projection, make_projection};
use clippy_utils::{ use clippy_utils::{
can_move_expr_to_closure, fn_def_id, get_enclosing_block, get_parent_node, higher, is_trait_method, path_to_local, can_move_expr_to_closure, fn_def_id, get_enclosing_block, higher, is_trait_method, path_to_local, path_to_local_id,
path_to_local_id, CaptureKind, CaptureKind,
}; };
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, MultiSpan}; use rustc_errors::{Applicability, MultiSpan};
@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
iter_expr: &'tcx Expr<'tcx>, iter_expr: &'tcx Expr<'tcx>,
call_span: Span, call_span: Span,
) { ) {
match get_parent_node(cx.tcx, collect_expr.hir_id) { match cx.tcx.parent_hir_node(collect_expr.hir_id) {
Node::Expr(parent) => { Node::Expr(parent) => {
check_collect_into_intoiterator(cx, parent, collect_expr, call_span, iter_expr); check_collect_into_intoiterator(cx, parent, collect_expr, call_span, iter_expr);

View File

@ -6,8 +6,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg; use clippy_utils::sugg::Sugg;
use clippy_utils::{ use clippy_utils::{
get_parent_node, higher, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt, span_extract_comment, higher, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt, span_extract_comment, SpanlessEq,
SpanlessEq,
}; };
use rustc_ast::ast::LitKind; use rustc_ast::ast::LitKind;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -138,7 +137,7 @@ fn condition_needs_parentheses(e: &Expr<'_>) -> bool {
fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool { fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
matches!( matches!(
get_parent_node(cx.tcx, id), cx.tcx.parent_hir_node(id),
Node::Stmt(..) | Node::Block(Block { stmts: &[], .. }) Node::Stmt(..) | Node::Block(Block { stmts: &[], .. })
) )
} }

View File

@ -2,7 +2,7 @@ use super::needless_pass_by_value::requires_exact_signature;
use clippy_utils::diagnostics::span_lint_hir_and_then; use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use clippy_utils::visitors::for_each_expr_with_closures; use clippy_utils::visitors::for_each_expr_with_closures;
use clippy_utils::{get_parent_node, inherits_cfg, is_from_proc_macro, is_self}; use clippy_utils::{inherits_cfg, is_from_proc_macro, is_self};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
@ -239,7 +239,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
&& let ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(expr).kind() && let ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(expr).kind()
&& let Some(def_id) = def_id.as_local() && let Some(def_id) = def_id.as_local()
{ {
if let Node::Expr(e) = get_parent_node(cx.tcx, expr.hir_id) if let Node::Expr(e) = cx.tcx.parent_hir_node(expr.hir_id)
&& let ExprKind::Call(call, _) = e.kind && let ExprKind::Call(call, _) = e.kind
&& call.hir_id == expr.hir_id && call.hir_id == expr.hir_id
{ {

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then}; use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
use clippy_utils::source::snippet_opt; use clippy_utils::source::snippet_opt;
use clippy_utils::ty::has_drop; use clippy_utils::ty::has_drop;
use clippy_utils::{any_parent_is_automatically_derived, get_parent_node, is_lint_allowed, path_to_local, peel_blocks}; use clippy_utils::{any_parent_is_automatically_derived, is_lint_allowed, path_to_local, peel_blocks};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::{ use rustc_hir::{
@ -140,7 +140,7 @@ impl NoEffect {
for parent in cx.tcx.hir().parent_iter(stmt.hir_id) { for parent in cx.tcx.hir().parent_iter(stmt.hir_id) {
if let Node::Item(item) = parent.1 if let Node::Item(item) = parent.1
&& let ItemKind::Fn(..) = item.kind && let ItemKind::Fn(..) = item.kind
&& let Node::Block(block) = get_parent_node(cx.tcx, stmt.hir_id) && let Node::Block(block) = cx.tcx.parent_hir_node(stmt.hir_id)
&& let [.., final_stmt] = block.stmts && let [.., final_stmt] = block.stmts
&& final_stmt.hir_id == stmt.hir_id && final_stmt.hir_id == stmt.hir_id
{ {

View File

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::ty::implements_trait; use clippy_utils::ty::implements_trait;
use clippy_utils::{get_parent_node, is_res_lang_ctor, last_path_segment, path_res, std_or_core}; use clippy_utils::{is_res_lang_ctor, last_path_segment, path_res, std_or_core};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_hir::{Expr, ExprKind, ImplItem, ImplItemKind, LangItem, Node, UnOp}; use rustc_hir::{Expr, ExprKind, ImplItem, ImplItemKind, LangItem, Node, UnOp};
@ -112,7 +112,7 @@ declare_lint_pass!(NonCanonicalImpls => [NON_CANONICAL_CLONE_IMPL, NON_CANONICAL
impl LateLintPass<'_> for NonCanonicalImpls { impl LateLintPass<'_> for NonCanonicalImpls {
#[expect(clippy::too_many_lines)] #[expect(clippy::too_many_lines)]
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
let Node::Item(item) = get_parent_node(cx.tcx, impl_item.hir_id()) else { let Node::Item(item) = cx.tcx.parent_hir_node(impl_item.hir_id()) else {
return; return;
}; };
let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else { let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else {

View File

@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{get_expr_use_or_unification_node, get_parent_node, path_def_id, path_to_local, path_to_local_id}; use clippy_utils::{get_expr_use_or_unification_node, path_def_id, path_to_local, path_to_local_id};
use core::cell::Cell; use core::cell::Cell;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -227,7 +227,7 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
} }
// `skip_params` is either `0` or `1` to skip the `self` parameter in trait functions. // `skip_params` is either `0` or `1` to skip the `self` parameter in trait functions.
// It can't be renamed, and it can't be removed without removing it from multiple functions. // It can't be renamed, and it can't be removed without removing it from multiple functions.
let (fn_id, fn_kind, skip_params) = match get_parent_node(cx.tcx, body.value.hir_id) { let (fn_id, fn_kind, skip_params) = match cx.tcx.parent_hir_node(body.value.hir_id) {
Node::Item(i) => (i.owner_id.to_def_id(), FnKind::Fn, 0), Node::Item(i) => (i.owner_id.to_def_id(), FnKind::Fn, 0),
Node::TraitItem(&TraitItem { Node::TraitItem(&TraitItem {
kind: TraitItemKind::Fn(ref sig, _), kind: TraitItemKind::Fn(ref sig, _),
@ -244,7 +244,7 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
.. ..
}) => { }) => {
#[allow(trivial_casts)] #[allow(trivial_casts)]
if let Node::Item(item) = get_parent_node(cx.tcx, owner_id.into()) if let Node::Item(item) = cx.tcx.parent_hir_node(owner_id.into())
&& let Some(trait_ref) = cx && let Some(trait_ref) = cx
.tcx .tcx
.impl_trait_ref(item.owner_id) .impl_trait_ref(item.owner_id)

View File

@ -6,9 +6,9 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{ use clippy_utils::{
eq_expr_value, get_parent_node, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item, eq_expr_value, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item, is_res_lang_ctor,
is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
peel_blocks_with_stmt, span_contains_comment, span_contains_comment,
}; };
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::Res; use rustc_hir::def::Res;
@ -289,7 +289,7 @@ impl QuestionMark {
{ {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability); let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Node::Stmt(_)); let requires_semi = matches!(cx.tcx.parent_hir_node(expr.hir_id), Node::Stmt(_));
let sugg = format!( let sugg = format!(
"{receiver_str}{}?{}", "{receiver_str}{}?{}",
if by_ref == ByRef::Yes { ".as_ref()" } else { "" }, if by_ref == ByRef::Yes { ".as_ref()" } else { "" },

View File

@ -1,7 +1,7 @@
use crate::rustc_lint::LintContext; use crate::rustc_lint::LintContext;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::get_parent_expr;
use clippy_utils::sugg::Sugg; use clippy_utils::sugg::Sugg;
use clippy_utils::{get_parent_expr, get_parent_node};
use hir::Param; use hir::Param;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
@ -200,7 +200,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
hint = hint.asyncify(); hint = hint.asyncify();
} }
let is_in_fn_call_arg = if let Node::Expr(expr) = get_parent_node(cx.tcx, expr.hir_id) { let is_in_fn_call_arg = if let Node::Expr(expr) = cx.tcx.parent_hir_node(expr.hir_id) {
matches!(expr.kind, hir::ExprKind::Call(_, _)) matches!(expr.kind, hir::ExprKind::Call(_, _))
} else { } else {
false false

View File

@ -1,8 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::match_libc_symbol;
use clippy_utils::source::snippet_with_context; use clippy_utils::source::snippet_with_context;
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
use clippy_utils::visitors::is_expr_unsafe; use clippy_utils::visitors::is_expr_unsafe;
use clippy_utils::{get_parent_node, match_libc_symbol};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, UnsafeSource}; use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, UnsafeSource};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for StrlenOnCStrings {
&& path.ident.name == sym::as_ptr && path.ident.name == sym::as_ptr
{ {
let ctxt = expr.span.ctxt(); let ctxt = expr.span.ctxt();
let span = match get_parent_node(cx.tcx, expr.hir_id) { let span = match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Block(&Block { Node::Block(&Block {
rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided),
span, span,

View File

@ -1,9 +1,9 @@
use std::ops::ControlFlow; use std::ops::ControlFlow;
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_lint_allowed;
use clippy_utils::source::walk_span_to_context; use clippy_utils::source::walk_span_to_context;
use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
use clippy_utils::{get_parent_node, is_lint_allowed};
use hir::HirId; use hir::HirId;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_hir as hir; use rustc_hir as hir;
@ -340,8 +340,8 @@ fn block_parents_have_safety_comment(
cx: &LateContext<'_>, cx: &LateContext<'_>,
id: hir::HirId, id: hir::HirId,
) -> bool { ) -> bool {
let (span, hir_id) = match get_parent_node(cx.tcx, id) { let (span, hir_id) = match cx.tcx.parent_hir_node(id) {
Node::Expr(expr) => match get_parent_node(cx.tcx, expr.hir_id) { Node::Expr(expr) => match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Local(hir::Local { span, hir_id, .. }) => (*span, *hir_id), Node::Local(hir::Local { span, hir_id, .. }) => (*span, *hir_id),
Node::Item(hir::Item { Node::Item(hir::Item {
kind: hir::ItemKind::Const(..) | ItemKind::Static(..), kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
@ -445,7 +445,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
if item.span.ctxt() != SyntaxContext::root() { if item.span.ctxt() != SyntaxContext::root() {
return HasSafetyComment::No; return HasSafetyComment::No;
} }
let comment_start = match get_parent_node(cx.tcx, item.hir_id()) { let comment_start = match cx.tcx.parent_hir_node(item.hir_id()) {
Node::Crate(parent_mod) => comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item), Node::Crate(parent_mod) => comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item),
Node::Item(parent_item) => { Node::Item(parent_item) => {
if let ItemKind::Mod(parent_mod) = &parent_item.kind { if let ItemKind::Mod(parent_mod) = &parent_item.kind {
@ -456,7 +456,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
} }
}, },
Node::Stmt(stmt) => { Node::Stmt(stmt) => {
if let Node::Block(block) = get_parent_node(cx.tcx, stmt.hir_id) { if let Node::Block(block) = cx.tcx.parent_hir_node(stmt.hir_id) {
walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo) walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo)
} else { } else {
// Problem getting the parent node. Pretend a comment was found. // Problem getting the parent node. Pretend a comment was found.
@ -504,7 +504,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
return HasSafetyComment::No; return HasSafetyComment::No;
} }
let comment_start = match get_parent_node(cx.tcx, hir_id) { let comment_start = match cx.tcx.parent_hir_node(hir_id) {
Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo),
_ => return HasSafetyComment::Maybe, _ => return HasSafetyComment::Maybe,
}; };

View File

@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_parent_node;
use clippy_utils::source::snippet_with_context; use clippy_utils::source::snippet_with_context;
use clippy_utils::visitors::{for_each_local_assignment, for_each_value_source}; use clippy_utils::visitors::{for_each_local_assignment, for_each_value_source};
use core::ops::ControlFlow; use core::ops::ControlFlow;
@ -103,7 +102,7 @@ fn expr_needs_inferred_result<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -
return false; return false;
} }
while let Some(id) = locals_to_check.pop() { while let Some(id) = locals_to_check.pop() {
if let Node::Local(l) = get_parent_node(cx.tcx, id) { if let Node::Local(l) = cx.tcx.parent_hir_node(id) {
if !l.ty.map_or(true, |ty| matches!(ty.kind, TyKind::Infer)) { if !l.ty.map_or(true, |ty| matches!(ty.kind, TyKind::Infer)) {
return false; return false;
} }

View File

@ -1307,11 +1307,6 @@ pub fn contains_return<'tcx>(expr: impl Visitable<'tcx>) -> bool {
.is_some() .is_some()
} }
/// Gets the parent node, if any.
pub fn get_parent_node(tcx: TyCtxt<'_>, id: HirId) -> Node<'_> {
tcx.parent_hir_node(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.
pub fn get_parent_expr<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>) -> Option<&'tcx Expr<'tcx>> { pub fn get_parent_expr<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
get_parent_expr_for_hir(cx, e.hir_id) get_parent_expr_for_hir(cx, e.hir_id)
@ -1320,7 +1315,7 @@ pub fn get_parent_expr<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>) -> Option<&'t
/// This retrieves the parent for the given `HirId` if it's an expression. This is useful for /// This retrieves the parent for the given `HirId` if it's an expression. This is useful for
/// constraint lints /// constraint lints
pub fn get_parent_expr_for_hir<'tcx>(cx: &LateContext<'tcx>, hir_id: hir::HirId) -> Option<&'tcx Expr<'tcx>> { pub fn get_parent_expr_for_hir<'tcx>(cx: &LateContext<'tcx>, hir_id: hir::HirId) -> Option<&'tcx Expr<'tcx>> {
match get_parent_node(cx.tcx, hir_id) { match cx.tcx.parent_hir_node(hir_id) {
Node::Expr(parent) => Some(parent), Node::Expr(parent) => Some(parent),
_ => None, _ => None,
} }
@ -2182,7 +2177,7 @@ pub fn is_expr_used_or_unified(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
/// Checks if the expression is the final expression returned from a block. /// Checks if the expression is the final expression returned from a block.
pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool { pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
matches!(get_parent_node(tcx, expr.hir_id), Node::Block(..)) matches!(tcx.parent_hir_node(expr.hir_id), Node::Block(..))
} }
pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> { pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {