Skip to content

Commit

Permalink
Move methods from Map to TyCtxt, part 2.
Browse files Browse the repository at this point in the history
Continuing the work started in #136466.

Every method gains a `hir_` prefix, though for the ones that already
have a `par_` or `try_par_` prefix I added the `hir_` after that.
  • Loading branch information
nnethercote committed Feb 17, 2025
1 parent 42114c9 commit 835e6a6
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 45 deletions.
3 changes: 1 addition & 2 deletions clippy_lints/src/default_numeric_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);

impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
let hir = cx.tcx.hir();
// NOTE: this is different from `clippy_utils::is_inside_always_const_context`.
// Inline const supports type inference.
let is_parent_const = matches!(
hir.body_const_context(hir.body_owner_def_id(body.id())),
cx.tcx.hir_body_const_context(cx.tcx.hir_body_owner_def_id(body.id())),
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))
);
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
if let ItemKind::Enum(def, _) = &item.kind {
for var in def.variants {
if let Some(anon_const) = &var.disr_expr {
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
let def_id = cx.tcx.hir_body_owner_def_id(anon_const.body);
let mut ty = cx.tcx.type_of(def_id.to_def_id()).instantiate_identity();
let constant = cx
.tcx
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/functions/impl_trait_in_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn report(cx: &LateContext<'_>, param: &GenericParam<'_>, generics: &Generics<'_

pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) {
if let FnKind::ItemFn(_, generics, _) = kind
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
&& cx.tcx.visibility(cx.tcx.hir_body_owner_def_id(body.id())).is_public()
&& !is_in_test(cx.tcx, hir_id)
{
for param in generics.params {
Expand All @@ -57,7 +57,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
&& let hir::Impl { of_trait, .. } = *impl_
&& of_trait.is_none()
&& let body = cx.tcx.hir_body(body_id)
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
&& cx.tcx.visibility(cx.tcx.hir_body_owner_def_id(body.id())).is_public()
&& !is_in_test(cx.tcx, impl_item.hir_id())
{
for param in impl_item.generics.params {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/functions/renamed_function_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &ImplItem<'_>, ignored
&& let Some(did) = trait_item_def_id_of_impl(items, item.owner_id)
&& !is_from_ignored_trait(trait_ref, ignored_traits)
{
let mut param_idents_iter = cx.tcx.hir().body_param_names(body_id);
let mut param_idents_iter = cx.tcx.hir_body_param_names(body_id);
let mut default_param_idents_iter = cx.tcx.fn_arg_names(did).iter().copied();

let renames = RenamedFnArgs::new(&mut default_param_idents_iter, &mut param_idents_iter);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/manual_float_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
&& exprs.iter_mut().partition_in_place(|i| path_to_local(i).is_some()) == 2
&& !expr.span.in_external_macro(cx.sess().source_map())
&& (
is_not_const(cx.tcx, cx.tcx.hir().enclosing_body_owner(expr.hir_id).into())
is_not_const(cx.tcx, cx.tcx.hir_enclosing_body_owner(expr.hir_id).into())
|| self.msrv.meets(msrvs::CONST_FLOAT_CLASSIFY)
)
&& let [first, second, const_1, const_2] = exprs
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ pub(super) fn check<'tcx>(
unwrap_or_span: unwrap_arg.span,
};

let map = cx.tcx.hir();
let body = map.body_owned_by(map.enclosing_body_owner(expr.hir_id));
let body = cx.tcx.hir_body_owned_by(cx.tcx.hir_enclosing_body_owner(expr.hir_id));

// Visit the body, and return if we've found a reference
if reference_visitor.visit_body(body).is_break() {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/needless_borrows_for_generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
if self
.possible_borrowers
.last()
.is_some_and(|&(local_def_id, _)| local_def_id == cx.tcx.hir().body_owner_def_id(body.id()))
.is_some_and(|&(local_def_id, _)| local_def_id == cx.tcx.hir_body_owner_def_id(body.id()))
{
self.possible_borrowers.pop();
}
Expand Down Expand Up @@ -359,7 +359,7 @@ fn referent_used_exactly_once<'tcx>(
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
&& !place.is_indirect_first_projection()
{
let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
let body_owner_local_def_id = cx.tcx.hir_enclosing_body_owner(reference.hir_id);
if possible_borrowers
.last()
.is_none_or(|&(local_def_id, _)| local_def_id != body_owner_local_def_id)
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/needless_pass_by_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl MutablyUsedVariablesCtxt<'_> {
fn is_in_unsafe_block(&self, item: HirId) -> bool {
let hir = self.tcx.hir();
for (parent, node) in hir.parent_iter(item) {
if let Some(fn_sig) = hir.fn_sig_by_hir_id(parent) {
if let Some(fn_sig) = self.tcx.hir_fn_sig_by_hir_id(parent) {
return fn_sig.header.is_unsafe();
} else if let Node::Block(block) = node {
if matches!(block.rules, BlockCheckMode::UnsafeBlock(_)) {
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
}

fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_owner_def_id = cx.tcx.hir_body_owner_def_id(body.id());

let body_owner_kind = cx.tcx.hir().body_owner_kind(body_owner_def_id);
let body_owner_kind = cx.tcx.hir_body_owner_kind(body_owner_def_id);
if let hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_) = body_owner_kind {
let body_span = cx.tcx.hir().span_with_body(body_owner);
if let Some(span) = self.const_span
Expand All @@ -365,7 +365,7 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
}

fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_span = cx.tcx.hir().span(body_owner);
if let Some(span) = self.const_span
&& span.contains(body_span)
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/operators/numeric_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl Context {
}

pub fn enter_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_owner_def_id = cx.tcx.hir_body_owner_def_id(body.id());

match cx.tcx.hir().body_owner_kind(body_owner_def_id) {
match cx.tcx.hir_body_owner_kind(body_owner_def_id) {
hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const { .. } => {
let body_span = cx.tcx.hir().span_with_body(body_owner);

Expand All @@ -87,7 +87,7 @@ impl Context {
}

pub fn body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_span = cx.tcx.hir().span_with_body(body_owner);

if let Some(span) = self.const_span {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/redundant_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
/// assert_static(closure);
/// ```
fn is_by_value_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
let closure_def_id = cx.tcx.hir().enclosing_body_owner(redefinition);
let closure_def_id = cx.tcx.hir_enclosing_body_owner(redefinition);

cx.tcx.is_closure_like(closure_def_id.to_def_id())
&& cx.tcx.closure_captures(closure_def_id).iter().any(|c| {
Expand Down
8 changes: 3 additions & 5 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,15 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
}

fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
let owner_id = hir.body_owner_def_id(body.id());
if !matches!(hir.body_owner_kind(owner_id), BodyOwnerKind::Closure) {
let owner_id = cx.tcx.hir_body_owner_def_id(body.id());
if !matches!(cx.tcx.hir_body_owner_kind(owner_id), BodyOwnerKind::Closure) {
self.bindings.push((FxHashMap::default(), owner_id));
}
}

fn check_body_post(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
if !matches!(
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
cx.tcx.hir_body_owner_kind(cx.tcx.hir_body_owner_def_id(body.id())),
BodyOwnerKind::Closure
) {
self.bindings.pop();
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/single_call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ impl SingleCallFn {
|| fn_span.in_external_macro(cx.sess().source_map())
|| cx
.tcx
.hir()
.maybe_body_owned_by(fn_def_id)
.hir_maybe_body_owned_by(fn_def_id)
.is_none_or(|body| is_in_test_function(cx.tcx, body.value.hir_id))
|| match cx.tcx.hir_node(fn_hir_id) {
Node::Item(item) => is_from_proc_macro(cx, item),
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/transmute/missing_transmute_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId)
}

fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool {
let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id);
if let Some(body) = cx.tcx.hir().maybe_body_owned_by(def_id) {
let def_id = cx.tcx.hir_enclosing_body_owner(expr_hir_id);
if let Some(body) = cx.tcx.hir_maybe_body_owned_by(def_id) {
return body.value.peel_blocks().hir_id == expr_hir_id;
}
false
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ impl UnconditionalRecursion {
&& let [return_expr] = get_return_calls_in_body(body).as_slice()
&& let ExprKind::Call(call_expr, _) = return_expr.kind
// We need to use typeck here to infer the actual function being called.
&& let body_def_id = cx.tcx.hir().enclosing_body_owner(call_expr.hir_id)
&& let Some(body_owner) = cx.tcx.hir().maybe_body_owned_by(body_def_id)
&& let body_def_id = cx.tcx.hir_enclosing_body_owner(call_expr.hir_id)
&& let Some(body_owner) = cx.tcx.hir_maybe_body_owned_by(body_def_id)
&& let typeck = cx.tcx.typeck_body(body_owner.id())
&& let Some(call_def_id) = typeck.type_dependent_def_id(call_expr.hir_id)
{
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
}

fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
let hir = cx.tcx.hir();
if let Some(body) = hir.maybe_body_owned_by(hir_id.expect_owner().def_id) {
if let Some(body) = cx.tcx.hir_maybe_body_owned_by(hir_id.expect_owner().def_id) {
check_node(cx, hir_id, |v| {
v.expr(&v.bind("expr", body.value));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
output: &mut self.registered_lints,
cx,
};
let body = cx.tcx.hir().body_owned_by(
let body = cx.tcx.hir_body_owned_by(
impl_item_refs
.iter()
.find(|iiref| iiref.ident.as_str() == "lint_vec")
Expand Down
10 changes: 4 additions & 6 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ pub fn is_in_const_context(cx: &LateContext<'_>) -> bool {
debug_assert!(cx.enclosing_body.is_some(), "`LateContext` has no enclosing body");
cx.enclosing_body.is_some_and(|id| {
cx.tcx
.hir()
.body_const_context(cx.tcx.hir().body_owner_def_id(id))
.hir_body_const_context(cx.tcx.hir_body_owner_def_id(id))
.is_some()
})
}
Expand All @@ -251,8 +250,7 @@ pub fn is_in_const_context(cx: &LateContext<'_>) -> bool {
/// * associated constants
pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
use ConstContext::{Const, ConstFn, Static};
let hir = tcx.hir();
let Some(ctx) = hir.body_const_context(hir.enclosing_body_owner(hir_id)) else {
let Some(ctx) = tcx.hir_body_const_context(tcx.hir_enclosing_body_owner(hir_id)) else {
return false;
};
match ctx {
Expand Down Expand Up @@ -1648,7 +1646,7 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool
if is_integer_literal(e, value) {
return true;
}
let enclosing_body = cx.tcx.hir().enclosing_body_owner(e.hir_id);
let enclosing_body = cx.tcx.hir_enclosing_body_owner(e.hir_id);
if let Some(Constant::Int(v)) =
ConstEvalCtxt::with_env(cx.tcx, cx.typing_env(), cx.tcx.typeck(enclosing_body)).eval(e)
{
Expand Down Expand Up @@ -2762,7 +2760,7 @@ impl<'tcx> ExprUseCtxt<'tcx> {

Node::Expr(use_expr) => match use_expr.kind {
ExprKind::Ret(_) => ExprUseNode::Return(OwnerId {
def_id: cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap()),
def_id: cx.tcx.hir_body_owner_def_id(cx.enclosing_body.unwrap()),
}),

ExprKind::Closure(closure) => ExprUseNode::Return(OwnerId { def_id: closure.def_id }),
Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ pub fn used_exactly_once(mir: &Body<'_>, local: Local) -> Option<bool> {
/// Returns the `mir::Body` containing the node associated with `hir_id`.
#[allow(clippy::module_name_repetitions)]
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<&Body<'_>> {
let body_owner_local_def_id = tcx.hir().enclosing_body_owner(hir_id);
if tcx.hir().body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
let body_owner_local_def_id = tcx.hir_enclosing_body_owner(hir_id);
if tcx.hir_body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
Some(tcx.optimized_mir(body_owner_local_def_id.to_def_id()))
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
// through calling `body_owner_kind`, which would panic if the callee
// does not have a body.
if let Some(callee_id) = callee_id {
let _ = tcx.hir().body_owner_kind(callee_id);
let _ = tcx.hir_body_owner_kind(callee_id);
}

let ty = tcx.erase_regions(ty);
Expand Down Expand Up @@ -705,7 +705,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'t
ty::Closure(id, subs) => {
let decl = id
.as_local()
.and_then(|id| cx.tcx.hir().fn_decl_by_hir_id(cx.tcx.local_def_id_to_hir_id(id)));
.and_then(|id| cx.tcx.hir_fn_decl_by_hir_id(cx.tcx.local_def_id_to_hir_id(id)));
Some(ExprFnSig::Closure(decl, subs.as_closure().sig()))
},
ty::FnDef(id, subs) => Some(ExprFnSig::Sig(cx.tcx.fn_sig(id).instantiate(cx.tcx, subs), Some(id))),
Expand Down

0 comments on commit 835e6a6

Please sign in to comment.