Skip to content

Commit a9fb00b

Browse files
committed
Auto merge of rust-lang#129975 - notriddle:notriddle/lint-skip, r=GuillaumeGomez
rustdoc: unify the short-circuit on all lints This is a bit of an experiment to see if it improves perf.
2 parents 6f7229c + d4b246b commit a9fb00b

7 files changed

+184
-202
lines changed

src/librustdoc/passes/lint.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@ pub(crate) fn run_lints(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
2727

2828
impl<'a, 'tcx> DocVisitor for Linter<'a, 'tcx> {
2929
fn visit_item(&mut self, item: &Item) {
30-
bare_urls::visit_item(self.cx, item);
31-
check_code_block_syntax::visit_item(self.cx, item);
32-
html_tags::visit_item(self.cx, item);
33-
unescaped_backticks::visit_item(self.cx, item);
34-
redundant_explicit_links::visit_item(self.cx, item);
35-
unportable_markdown::visit_item(self.cx, item);
30+
let Some(hir_id) = DocContext::as_local_hir_id(self.cx.tcx, item.item_id) else {
31+
// If non-local, no need to check anything.
32+
return;
33+
};
34+
let dox = item.doc_value();
35+
if !dox.is_empty() {
36+
let may_have_link = dox.contains(&[':', '['][..]);
37+
let may_have_block_comment_or_html = dox.contains(&['<', '>']);
38+
// ~~~rust
39+
// // This is a real, supported commonmark syntax for block code
40+
// ~~~
41+
let may_have_code = dox.contains(&['~', '`', '\t'][..]) || dox.contains(" ");
42+
if may_have_link {
43+
bare_urls::visit_item(self.cx, item, hir_id, &dox);
44+
redundant_explicit_links::visit_item(self.cx, item, hir_id);
45+
}
46+
if may_have_code {
47+
check_code_block_syntax::visit_item(self.cx, item, &dox);
48+
unescaped_backticks::visit_item(self.cx, item, hir_id, &dox);
49+
}
50+
if may_have_block_comment_or_html {
51+
html_tags::visit_item(self.cx, item, hir_id, &dox);
52+
unportable_markdown::visit_item(self.cx, item, hir_id, &dox);
53+
} else if may_have_link {
54+
unportable_markdown::visit_item(self.cx, item, hir_id, &dox);
55+
}
56+
}
3657

3758
self.visit_item_recur(item)
3859
}

src/librustdoc/passes/lint/bare_urls.rs

+31-37
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,51 @@ use std::sync::LazyLock;
88
use pulldown_cmark::{Event, Parser, Tag};
99
use regex::Regex;
1010
use rustc_errors::Applicability;
11+
use rustc_hir::HirId;
1112
use rustc_resolve::rustdoc::source_span_for_markdown_range;
1213
use tracing::trace;
1314

1415
use crate::clean::*;
1516
use crate::core::DocContext;
1617
use crate::html::markdown::main_body_opts;
1718

18-
pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
19-
let Some(hir_id) = DocContext::as_local_hir_id(cx.tcx, item.item_id) else {
20-
// If non-local, no need to check anything.
21-
return;
19+
pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &str) {
20+
let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
21+
let sp = source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
22+
.unwrap_or_else(|| item.attr_span(cx.tcx));
23+
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
24+
lint.primary_message(msg)
25+
.note("bare URLs are not automatically turned into clickable links")
26+
.multipart_suggestion(
27+
"use an automatic link instead",
28+
vec![
29+
(sp.shrink_to_lo(), "<".to_string()),
30+
(sp.shrink_to_hi(), ">".to_string()),
31+
],
32+
Applicability::MachineApplicable,
33+
);
34+
});
2235
};
23-
let dox = item.doc_value();
24-
if !dox.is_empty() {
25-
let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
26-
let sp = source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
27-
.unwrap_or_else(|| item.attr_span(cx.tcx));
28-
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
29-
lint.primary_message(msg)
30-
.note("bare URLs are not automatically turned into clickable links")
31-
.multipart_suggestion(
32-
"use an automatic link instead",
33-
vec![
34-
(sp.shrink_to_lo(), "<".to_string()),
35-
(sp.shrink_to_hi(), ">".to_string()),
36-
],
37-
Applicability::MachineApplicable,
38-
);
39-
});
40-
};
4136

42-
let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
37+
let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
4338

44-
while let Some((event, range)) = p.next() {
45-
match event {
46-
Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
47-
// We don't want to check the text inside code blocks or links.
48-
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
49-
while let Some((event, _)) = p.next() {
50-
match event {
51-
Event::End(end)
52-
if mem::discriminant(&end) == mem::discriminant(&tag.to_end()) =>
53-
{
54-
break;
55-
}
56-
_ => {}
39+
while let Some((event, range)) = p.next() {
40+
match event {
41+
Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
42+
// We don't want to check the text inside code blocks or links.
43+
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
44+
while let Some((event, _)) = p.next() {
45+
match event {
46+
Event::End(end)
47+
if mem::discriminant(&end) == mem::discriminant(&tag.to_end()) =>
48+
{
49+
break;
5750
}
51+
_ => {}
5852
}
5953
}
60-
_ => {}
6154
}
55+
_ => {}
6256
}
6357
}
6458
}

src/librustdoc/passes/lint/check_code_block_syntax.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ use crate::clean;
1515
use crate::core::DocContext;
1616
use crate::html::markdown::{self, RustCodeBlock};
1717

18-
pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item) {
19-
if let Some(def_id) = item.item_id.as_local_def_id()
20-
&& let Some(dox) = &item.opt_doc_value()
21-
{
18+
pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item, dox: &str) {
19+
if let Some(def_id) = item.item_id.as_local_def_id() {
2220
let sp = item.attr_span(cx.tcx);
2321
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, def_id, sp);
2422
for code_block in markdown::rust_code_blocks(dox, &extra) {

0 commit comments

Comments
 (0)