1
Fork 0

coverage: Get hole spans from nested items without fully visiting them

It turns out that this visitor doesn't actually need `nested_filter::All` to
handle nested items; it just needs to override `visit_nested_item` and look up
the item's span.
This commit is contained in:
Zalathar 2025-02-19 00:54:47 +11:00
parent d9b91de00c
commit 51f704f0ff

View file

@ -352,19 +352,20 @@ fn extract_hole_spans_from_hir<'tcx>(
} }
impl<'hir, F: FnMut(Span)> Visitor<'hir> for HolesVisitor<'hir, F> { impl<'hir, F: FnMut(Span)> Visitor<'hir> for HolesVisitor<'hir, F> {
/// - We need `NestedFilter::INTRA = true` so that `visit_item` will be called. /// We have special handling for nested items, but we still want to
/// - Bodies of nested items don't actually get visited, because of the /// traverse into nested bodies of things that are not considered items,
/// `visit_item` override. /// such as "anon consts" (e.g. array lengths).
/// - For nested bodies that are not part of an item, we do want to visit any type NestedFilter = nested_filter::OnlyBodies;
/// items contained within them.
type NestedFilter = nested_filter::All;
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.tcx self.tcx
} }
fn visit_item(&mut self, item: &'hir hir::Item<'hir>) { /// We override `visit_nested_item` instead of `visit_item` because we
(self.visit_hole_span)(item.span); /// only need the item's span, not the item itself.
fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
let span = self.tcx.def_span(id.owner_id.def_id);
(self.visit_hole_span)(span);
// Having visited this item, we don't care about its children, // Having visited this item, we don't care about its children,
// so don't call `walk_item`. // so don't call `walk_item`.
} }