Skip to content

Commit

Permalink
Overhaul the intravisit::Map trait.
Browse files Browse the repository at this point in the history
First of all, note that `Map` has three different relevant meanings.
- The `intravisit::Map` trait.
- The `map::Map` struct.
- The `NestedFilter::Map` associated type.

The `intravisit::Map` trait is impl'd twice.
- For `!`, where the methods are all unreachable.
- For `map::Map`, which gets HIR stuff from the `TyCtxt`.

As part of getting rid of `map::Map`, this commit changes `impl
intravisit::Map for map::Map` to `impl intravisit::Map for TyCtxt`. It's
fairly straightforward except various things are renamed, because the
existing names would no longer have made sense.

- `trait intravisit::Map` becomes `trait intravisit::HirTyCtxt`, so named
  because it gets some HIR stuff from a `TyCtxt`.
- `NestedFilter::Map` assoc type becomes `NestedFilter::MaybeTyCtxt`,
  because it's always `!` or `TyCtxt`.
- `Visitor::nested_visit_map` becomes `Visitor::maybe_tcx`.

I deliberately made the new trait and associated type names different to
avoid the old `type Map: Map` situation, which I found confusing. We now
have `type MaybeTyCtxt: HirTyCtxt`.
  • Loading branch information
nnethercote committed Feb 17, 2025
1 parent 8cf9eea commit 91481e3
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 64 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
walk_expr(self, expr)
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,8 @@ impl<'tcx> Visitor<'tcx> for FindPanicUnwrap<'_, 'tcx> {
// Panics in const blocks will cause compilation to fail.
fn visit_anon_const(&mut self, _: &'tcx AnonConst) {}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/extra_unused_type_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl<'tcx> Visitor<'tcx> for TypeWalker<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/from_over_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl<'tcx> Visitor<'tcx> for SelfFinder<'_, 'tcx> {
type Result = ControlFlow<()>;
type NestedFilter = OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) -> Self::Result {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/implicit_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl<'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'_, '_, 'tcx> {
walk_expr(self, e);
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}
4 changes: 2 additions & 2 deletions clippy_lints/src/index_refutable_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ struct SliceIndexLintingVisitor<'a, 'tcx> {
impl<'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'_, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use rustc_hir::{
WherePredicateKind, lang_items,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter as middle_nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
Expand Down Expand Up @@ -582,7 +582,7 @@ impl<'tcx, F> Visitor<'tcx> for LifetimeChecker<'_, 'tcx, F>
where
F: NestedFilter<'tcx>,
{
type Map = Map<'tcx>;
type MaybeTyCtxt = TyCtxt<'tcx>;
type NestedFilter = F;

// for lifetimes as parameters of generics
Expand Down Expand Up @@ -628,8 +628,8 @@ where
self.lifetime_elision_impossible = false;
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/loops/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ impl<'tcx> Visitor<'tcx> for InitializeVisitor<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/loops/while_let_on_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
impl<'tcx> Visitor<'tcx> for AfterLoopVisitor<'_, '_, 'tcx> {
type NestedFilter = OnlyBodies;
type Result = ControlFlow<()>;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, e: &'tcx Expr<'_>) -> Self::Result {
Expand Down Expand Up @@ -288,8 +288,8 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
}
impl<'tcx> Visitor<'tcx> for NestedLoopVisitor<'_, '_, 'tcx> {
type NestedFilter = OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_local(&mut self, l: &'tcx LetStmt<'_>) {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/needless_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ impl<'tcx> Visitor<'tcx> for UsedCountVisitor<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ impl<'tcx> Visitor<'tcx> for UnwrapVisitor<'_, 'tcx> {
walk_path(self, path);
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ impl<'tcx> Visitor<'tcx> for ReferenceVisitor<'_, 'tcx> {
rustc_hir::intravisit::walk_expr(self, expr)
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ struct CloneOrCopyVisitor<'cx, 'tcx> {
impl<'tcx> Visitor<'tcx> for CloneOrCopyVisitor<'_, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/mutable_debug_assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'tcx> Visitor<'tcx> for MutArgVisitor<'_, 'tcx> {
walk_expr(self, expr);
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}
4 changes: 2 additions & 2 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, args: &[
}
impl<'tcx> Visitor<'tcx> for V<'_, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_anon_const(&mut self, _: &'tcx AnonConst) {}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/redundant_closure_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
hir_visit::walk_expr(self, expr);
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}
let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
Expand Down
7 changes: 2 additions & 5 deletions clippy_lints/src/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_hir::intravisit::{FnKind, Visitor, walk_body, walk_expr};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Node, QPath, TyKind};
use rustc_hir_analysis::lower_ty;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, AssocKind, Ty, TyCtxt};
use rustc_session::impl_lint_pass;
Expand Down Expand Up @@ -275,7 +274,6 @@ fn is_default_method_on_current_ty<'tcx>(tcx: TyCtxt<'tcx>, qpath: QPath<'tcx>,

struct CheckCalls<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
map: Map<'tcx>,
implemented_ty_id: DefId,
method_span: Span,
}
Expand All @@ -287,8 +285,8 @@ where
type NestedFilter = nested_filter::OnlyBodies;
type Result = ControlFlow<()>;

fn nested_visit_map(&mut self) -> Self::Map {
self.map
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) -> ControlFlow<()> {
Expand Down Expand Up @@ -380,7 +378,6 @@ impl UnconditionalRecursion {
{
let mut c = CheckCalls {
cx,
map: cx.tcx.hir(),
implemented_ty_id,
method_span,
};
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/unused_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl<'tcx> Visitor<'tcx> for AsyncFnVisitor<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/unused_peekable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
type NestedFilter = OnlyBodies;
type Result = ControlFlow<()>;

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) -> ControlFlow<()> {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl<'tcx> Visitor<'tcx> for LintCollector<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx.hir()
}
}
4 changes: 2 additions & 2 deletions clippy_lints/src/zombie_processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ impl<'tcx> Visitor<'tcx> for WaitFinder<'_, 'tcx> {
walk_expr(self, ex)
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,8 @@ impl<'tcx> Visitor<'tcx> for ContainsName<'_, 'tcx> {
}
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ impl<'tcx> Visitor<'tcx> for BindingUsageFinder<'_, 'tcx> {
ControlFlow::Continue(())
}

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
}

Expand Down
20 changes: 10 additions & 10 deletions clippy_utils/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ pub fn for_each_expr<'tcx, B, C: Continue>(
type NestedFilter = nested_filter::OnlyBodies;
type Result = ControlFlow<B>;

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.tcx
}

fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) -> Self::Result {
Expand Down Expand Up @@ -412,8 +412,8 @@ pub fn is_expr_unsafe<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
type NestedFilter = nested_filter::OnlyBodies;
type Result = ControlFlow<()>;

fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
fn visit_expr(&mut self, e: &'tcx Expr<'_>) -> Self::Result {
match e.kind {
Expand Down Expand Up @@ -477,8 +477,8 @@ pub fn contains_unsafe_block<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
impl<'tcx> Visitor<'tcx> for V<'_, 'tcx> {
type Result = ControlFlow<()>;
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_block(&mut self, b: &'tcx Block<'_>) -> Self::Result {
Expand Down Expand Up @@ -544,8 +544,8 @@ pub fn for_each_local_use_after_expr<'tcx, B>(
}
impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> ControlFlow<B>, B> Visitor<'tcx> for V<'_, 'tcx, F, B> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) {
Expand Down Expand Up @@ -729,8 +729,8 @@ pub fn for_each_local_assignment<'tcx, B>(
}
impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> ControlFlow<B>, B> Visitor<'tcx> for V<'_, 'tcx, F, B> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}

fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) {
Expand Down

0 comments on commit 91481e3

Please sign in to comment.