Skip to content

Commit b742870

Browse files
committed
Convert many impls of DocFolder to DocVisitor
Many of `DocFolder`'s impls didn't actually transform the syntax tree, so they can be visitors instead.
1 parent 10606c3 commit b742870

File tree

5 files changed

+39
-38
lines changed

5 files changed

+39
-38
lines changed

src/librustdoc/passes/bare_urls.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::Pass;
22
use crate::clean::*;
33
use crate::core::DocContext;
4-
use crate::fold::DocFolder;
54
use crate::html::markdown::main_body_opts;
5+
use crate::visit::DocVisitor;
66
use core::ops::Range;
77
use pulldown_cmark::{Event, Parser, Tag};
88
use regex::Regex;
@@ -53,16 +53,17 @@ impl<'a, 'tcx> BareUrlsLinter<'a, 'tcx> {
5353
}
5454

5555
crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
56-
BareUrlsLinter { cx }.fold_crate(krate)
56+
BareUrlsLinter { cx }.visit_crate(&krate);
57+
krate
5758
}
5859

59-
impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> {
60-
fn fold_item(&mut self, item: Item) -> Option<Item> {
60+
impl<'a, 'tcx> DocVisitor for BareUrlsLinter<'a, 'tcx> {
61+
fn visit_item(&mut self, item: &Item) {
6162
let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) {
6263
Some(hir_id) => hir_id,
6364
None => {
6465
// If non-local, no need to check anything.
65-
return Some(self.fold_item_recur(item));
66+
return;
6667
}
6768
};
6869
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
@@ -106,6 +107,6 @@ impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> {
106107
}
107108
}
108109

109-
Some(self.fold_item_recur(item))
110+
self.visit_item_recur(item)
110111
}
111112
}

src/librustdoc/passes/calculate_doc_coverage.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::clean;
22
use crate::core::DocContext;
3-
use crate::fold::{self, DocFolder};
43
use crate::html::markdown::{find_testable_code, ErrorCodes};
54
use crate::passes::check_doc_test_visibility::{should_have_doc_example, Tests};
65
use crate::passes::Pass;
6+
use crate::visit::DocVisitor;
77
use rustc_hir as hir;
88
use rustc_lint::builtin::MISSING_DOCS;
99
use rustc_middle::lint::LintLevelSource;
@@ -23,7 +23,7 @@ crate const CALCULATE_DOC_COVERAGE: Pass = Pass {
2323

2424
fn calculate_doc_coverage(krate: clean::Crate, ctx: &mut DocContext<'_>) -> clean::Crate {
2525
let mut calc = CoverageCalculator { items: Default::default(), ctx };
26-
let krate = calc.fold_crate(krate);
26+
calc.visit_crate(&krate);
2727

2828
calc.print_results();
2929

@@ -182,17 +182,18 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> {
182182
}
183183
}
184184

185-
impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
186-
fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> {
185+
impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
186+
fn visit_item(&mut self, i: &clean::Item) {
187+
if !i.def_id.is_local() {
188+
// non-local items are skipped because they can be out of the users control,
189+
// especially in the case of trait impls, which rustdoc eagerly inlines
190+
return;
191+
}
192+
187193
match *i.kind {
188-
_ if !i.def_id.is_local() => {
189-
// non-local items are skipped because they can be out of the users control,
190-
// especially in the case of trait impls, which rustdoc eagerly inlines
191-
return Some(i);
192-
}
193194
clean::StrippedItem(..) => {
194195
// don't count items in stripped modules
195-
return Some(i);
196+
return;
196197
}
197198
// docs on `use` and `extern crate` statements are not displayed, so they're not
198199
// worth counting
@@ -269,6 +270,6 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
269270
}
270271
}
271272

272-
Some(self.fold_item_recur(i))
273+
self.visit_item_recur(i)
273274
}
274275
}

src/librustdoc/passes/check_code_block_syntax.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_span::{hygiene::AstPass, ExpnData, ExpnKind, FileName, InnerSpan, DUMM
88

99
use crate::clean;
1010
use crate::core::DocContext;
11-
use crate::fold::DocFolder;
1211
use crate::html::markdown::{self, RustCodeBlock};
1312
use crate::passes::Pass;
13+
use crate::visit::DocVisitor;
1414

1515
crate const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
1616
name: "check-code-block-syntax",
@@ -19,7 +19,8 @@ crate const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
1919
};
2020

2121
crate fn check_code_block_syntax(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
22-
SyntaxChecker { cx }.fold_crate(krate)
22+
SyntaxChecker { cx }.visit_crate(&krate);
23+
krate
2324
}
2425

2526
struct SyntaxChecker<'a, 'tcx> {
@@ -141,8 +142,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
141142
}
142143
}
143144

144-
impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
145-
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
145+
impl<'a, 'tcx> DocVisitor for SyntaxChecker<'a, 'tcx> {
146+
fn visit_item(&mut self, item: &clean::Item) {
146147
if let Some(dox) = &item.attrs.collapsed_doc_value() {
147148
let sp = item.attr_span(self.cx.tcx);
148149
let extra = crate::html::markdown::ExtraInfo::new_did(
@@ -155,7 +156,7 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
155156
}
156157
}
157158

158-
Some(self.fold_item_recur(item))
159+
self.visit_item_recur(item)
159160
}
160161
}
161162

src/librustdoc/passes/check_doc_test_visibility.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use super::Pass;
77
use crate::clean;
88
use crate::clean::*;
99
use crate::core::DocContext;
10-
use crate::fold::DocFolder;
1110
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
11+
use crate::visit::DocVisitor;
1212
use crate::visit_ast::inherits_doc_hidden;
1313
use rustc_hir as hir;
1414
use rustc_middle::lint::LintLevelSource;
@@ -27,17 +27,17 @@ struct DocTestVisibilityLinter<'a, 'tcx> {
2727

2828
crate fn check_doc_test_visibility(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
2929
let mut coll = DocTestVisibilityLinter { cx };
30-
31-
coll.fold_crate(krate)
30+
coll.visit_crate(&krate);
31+
krate
3232
}
3333

34-
impl<'a, 'tcx> DocFolder for DocTestVisibilityLinter<'a, 'tcx> {
35-
fn fold_item(&mut self, item: Item) -> Option<Item> {
34+
impl<'a, 'tcx> DocVisitor for DocTestVisibilityLinter<'a, 'tcx> {
35+
fn visit_item(&mut self, item: &Item) {
3636
let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new);
3737

3838
look_for_tests(self.cx, &dox, &item);
3939

40-
Some(self.fold_item_recur(item))
40+
self.visit_item_recur(item)
4141
}
4242
}
4343

src/librustdoc/passes/html_tags.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::Pass;
22
use crate::clean::*;
33
use crate::core::DocContext;
4-
use crate::fold::DocFolder;
54
use crate::html::markdown::main_body_opts;
5+
use crate::visit::DocVisitor;
66
use core::ops::Range;
77
use pulldown_cmark::{Event, Parser, Tag};
88
use std::iter::Peekable;
@@ -19,13 +19,11 @@ struct InvalidHtmlTagsLinter<'a, 'tcx> {
1919
}
2020

2121
crate fn check_invalid_html_tags(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
22-
if !cx.tcx.sess.is_nightly_build() {
23-
krate
24-
} else {
22+
if cx.tcx.sess.is_nightly_build() {
2523
let mut coll = InvalidHtmlTagsLinter { cx };
26-
27-
coll.fold_crate(krate)
24+
coll.visit_crate(&krate);
2825
}
26+
krate
2927
}
3028

3129
const ALLOWED_UNCLOSED: &[&str] = &[
@@ -165,14 +163,14 @@ fn extract_tags(
165163
}
166164
}
167165

168-
impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
169-
fn fold_item(&mut self, item: Item) -> Option<Item> {
166+
impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
167+
fn visit_item(&mut self, item: &Item) {
170168
let tcx = self.cx.tcx;
171169
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) {
172170
Some(hir_id) => hir_id,
173171
None => {
174172
// If non-local, no need to check anything.
175-
return Some(self.fold_item_recur(item));
173+
return;
176174
}
177175
};
178176
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
@@ -217,6 +215,6 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
217215
}
218216
}
219217

220-
Some(self.fold_item_recur(item))
218+
self.visit_item_recur(item)
221219
}
222220
}

0 commit comments

Comments
 (0)