Skip to content

Commit 6ecacf7

Browse files
committed
rustdoc: adopt to the new lint API
1 parent 7e90a41 commit 6ecacf7

7 files changed

+61
-78
lines changed

src/librustdoc/core.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,8 @@ pub(crate) fn run_global_ctxt(
404404
tcx.struct_lint_node(
405405
crate::lint::MISSING_CRATE_LEVEL_DOCS,
406406
DocContext::as_local_hir_id(tcx, krate.module.item_id).unwrap(),
407-
|lint| {
408-
let mut diag =
409-
lint.build("no documentation found for this crate's top-level module");
410-
diag.help(&help);
411-
diag.emit();
412-
},
407+
"no documentation found for this crate's top-level module",
408+
|lint| lint.help(help),
413409
);
414410
}
415411

src/librustdoc/html/markdown.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,8 @@ impl<'tcx> ExtraInfo<'tcx> {
813813
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
814814
hir_id,
815815
self.sp,
816-
|lint| {
817-
let mut diag = lint.build(msg);
818-
diag.help(help);
819-
diag.emit();
820-
},
816+
msg,
817+
|lint| lint.help(help),
821818
);
822819
}
823820
}

src/librustdoc/passes/bare_urls.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,14 @@ impl<'a, 'tcx> DocVisitor for BareUrlsLinter<'a, 'tcx> {
7171
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
7272
let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
7373
.unwrap_or_else(|| item.attr_span(cx.tcx));
74-
cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, |lint| {
75-
lint.build(msg)
76-
.note("bare URLs are not automatically turned into clickable links")
74+
cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, msg, |lint| {
75+
lint.note("bare URLs are not automatically turned into clickable links")
7776
.span_suggestion(
7877
sp,
7978
"use an automatic link instead",
8079
format!("<{}>", url),
8180
Applicability::MachineApplicable,
8281
)
83-
.emit();
8482
});
8583
};
8684

src/librustdoc/passes/check_code_block_syntax.rs

+41-44
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use rustc_data_structures::sync::{Lock, Lrc};
33
use rustc_errors::{
44
emitter::Emitter, translation::Translate, Applicability, Diagnostic, Handler,
5-
LazyFallbackBundle, LintDiagnosticBuilder,
5+
LazyFallbackBundle,
66
};
77
use rustc_parse::parse_stream_from_source_str;
88
use rustc_session::parse::ParseSess;
@@ -97,48 +97,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
9797
None => (item.attr_span(self.cx.tcx), false),
9898
};
9999

100-
// lambda that will use the lint to start a new diagnostic and add
101-
// a suggestion to it when needed.
102-
let diag_builder = |lint: LintDiagnosticBuilder<'_, ()>| {
103-
let explanation = if is_ignore {
104-
"`ignore` code blocks require valid Rust code for syntax highlighting; \
105-
mark blocks that do not contain Rust code as text"
106-
} else {
107-
"mark blocks that do not contain Rust code as text"
108-
};
109-
let msg = if buffer.has_errors {
110-
"could not parse code block as Rust code"
111-
} else {
112-
"Rust code block is empty"
113-
};
114-
let mut diag = lint.build(msg);
115-
116-
if precise_span {
117-
if is_ignore {
118-
// giving an accurate suggestion is hard because `ignore` might not have come first in the list.
119-
// just give a `help` instead.
120-
diag.span_help(
121-
sp.from_inner(InnerSpan::new(0, 3)),
122-
&format!("{}: ```text", explanation),
123-
);
124-
} else if empty_block {
125-
diag.span_suggestion(
126-
sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
127-
explanation,
128-
"text",
129-
Applicability::MachineApplicable,
130-
);
131-
}
132-
} else if empty_block || is_ignore {
133-
diag.help(&format!("{}: ```text", explanation));
134-
}
135-
136-
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
137-
for message in buffer.messages.iter() {
138-
diag.note(message);
139-
}
140-
141-
diag.emit();
100+
let msg = if buffer.has_errors {
101+
"could not parse code block as Rust code"
102+
} else {
103+
"Rust code block is empty"
142104
};
143105

144106
// Finally build and emit the completed diagnostic.
@@ -148,7 +110,42 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
148110
crate::lint::INVALID_RUST_CODEBLOCKS,
149111
hir_id,
150112
sp,
151-
diag_builder,
113+
msg,
114+
|lint| {
115+
let explanation = if is_ignore {
116+
"`ignore` code blocks require valid Rust code for syntax highlighting; \
117+
mark blocks that do not contain Rust code as text"
118+
} else {
119+
"mark blocks that do not contain Rust code as text"
120+
};
121+
122+
if precise_span {
123+
if is_ignore {
124+
// giving an accurate suggestion is hard because `ignore` might not have come first in the list.
125+
// just give a `help` instead.
126+
lint.span_help(
127+
sp.from_inner(InnerSpan::new(0, 3)),
128+
&format!("{}: ```text", explanation),
129+
);
130+
} else if empty_block {
131+
lint.span_suggestion(
132+
sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
133+
explanation,
134+
"text",
135+
Applicability::MachineApplicable,
136+
);
137+
}
138+
} else if empty_block || is_ignore {
139+
lint.help(&format!("{}: ```text", explanation));
140+
}
141+
142+
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
143+
for message in buffer.messages.iter() {
144+
lint.note(message);
145+
}
146+
147+
lint
148+
},
152149
);
153150
}
154151
}

src/librustdoc/passes/check_doc_test_visibility.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ pub(crate) fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item
125125
crate::lint::MISSING_DOC_CODE_EXAMPLES,
126126
hir_id,
127127
sp,
128-
|lint| {
129-
lint.build("missing code example in this documentation").emit();
130-
},
128+
"missing code example in this documentation",
129+
|lint| lint,
131130
);
132131
}
133132
} else if tests.found_tests > 0
@@ -137,9 +136,8 @@ pub(crate) fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item
137136
crate::lint::PRIVATE_DOC_TESTS,
138137
hir_id,
139138
item.attr_span(cx.tcx),
140-
|lint| {
141-
lint.build("documentation test in private item").emit();
142-
},
139+
"documentation test in private item",
140+
|lint| lint,
143141
);
144142
}
145143
}

src/librustdoc/passes/collect_intra_doc_links.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1609,9 +1609,7 @@ fn report_diagnostic(
16091609

16101610
let sp = item.attr_span(tcx);
16111611

1612-
tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
1613-
let mut diag = lint.build(msg);
1614-
1612+
tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |lint| {
16151613
let span =
16161614
super::source_span_for_markdown_range(tcx, dox, link_range, &item.attrs).map(|sp| {
16171615
if dox.as_bytes().get(link_range.start) == Some(&b'`')
@@ -1624,7 +1622,7 @@ fn report_diagnostic(
16241622
});
16251623

16261624
if let Some(sp) = span {
1627-
diag.set_span(sp);
1625+
lint.set_span(sp);
16281626
} else {
16291627
// blah blah blah\nblah\nblah [blah] blah blah\nblah blah
16301628
// ^ ~~~~
@@ -1634,7 +1632,7 @@ fn report_diagnostic(
16341632
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
16351633

16361634
// Print the line containing the `link_range` and manually mark it with '^'s.
1637-
diag.note(&format!(
1635+
lint.note(&format!(
16381636
"the link appears in this line:\n\n{line}\n\
16391637
{indicator: <before$}{indicator:^<found$}",
16401638
line = line,
@@ -1644,9 +1642,9 @@ fn report_diagnostic(
16441642
));
16451643
}
16461644

1647-
decorate(&mut diag, span);
1645+
decorate(lint, span);
16481646

1649-
diag.emit();
1647+
lint
16501648
});
16511649
}
16521650

src/librustdoc/passes/html_tags.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
240240
Some(sp) => sp,
241241
None => item.attr_span(tcx),
242242
};
243-
tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
243+
tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, msg, |lint| {
244244
use rustc_lint_defs::Applicability;
245-
let mut diag = lint.build(msg);
246245
// If a tag looks like `<this>`, it might actually be a generic.
247246
// We don't try to detect stuff `<like, this>` because that's not valid HTML,
248247
// and we don't try to detect stuff `<like this>` because that's not valid Rust.
@@ -305,11 +304,10 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
305304
if (generics_start > 0 && dox.as_bytes()[generics_start - 1] == b'<')
306305
|| (generics_end < dox.len() && dox.as_bytes()[generics_end] == b'>')
307306
{
308-
diag.emit();
309-
return;
307+
return lint;
310308
}
311309
// multipart form is chosen here because ``Vec<i32>`` would be confusing.
312-
diag.multipart_suggestion(
310+
lint.multipart_suggestion(
313311
"try marking as source code",
314312
vec![
315313
(generics_sp.shrink_to_lo(), String::from("`")),
@@ -318,7 +316,8 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
318316
Applicability::MaybeIncorrect,
319317
);
320318
}
321-
diag.emit()
319+
320+
lint
322321
});
323322
};
324323

0 commit comments

Comments
 (0)