Skip to content

Commit 0e0ae47

Browse files
committed
Add a new lint to rustdoc for invalid codeblocks
This will make rustdoc behave properly when -Dwarnings is given
1 parent 3d6705a commit 0e0ae47

File tree

6 files changed

+102
-23
lines changed

6 files changed

+102
-23
lines changed

compiler/rustc_lint/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_middle::ty::TyCtxt;
7070
use rustc_session::lint::builtin::{
7171
BARE_TRAIT_OBJECTS, BROKEN_INTRA_DOC_LINKS, ELIDED_LIFETIMES_IN_PATHS,
7272
EXPLICIT_OUTLIVES_REQUIREMENTS, INVALID_CODEBLOCK_ATTRIBUTES, INVALID_HTML_TAGS,
73-
MISSING_DOC_CODE_EXAMPLES, NON_AUTOLINKS, PRIVATE_DOC_TESTS,
73+
INVALID_RUST_CODEBLOCK, MISSING_DOC_CODE_EXAMPLES, NON_AUTOLINKS, PRIVATE_DOC_TESTS,
7474
};
7575
use rustc_span::symbol::{Ident, Symbol};
7676
use rustc_span::Span;
@@ -320,6 +320,7 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
320320
BROKEN_INTRA_DOC_LINKS,
321321
PRIVATE_INTRA_DOC_LINKS,
322322
INVALID_CODEBLOCK_ATTRIBUTES,
323+
INVALID_RUST_CODEBLOCK,
323324
MISSING_DOC_CODE_EXAMPLES,
324325
PRIVATE_DOC_TESTS,
325326
INVALID_HTML_TAGS

compiler/rustc_lint_defs/src/builtin.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,18 @@ declare_lint! {
18471847
"codeblock attribute looks a lot like a known one"
18481848
}
18491849

1850+
declare_lint! {
1851+
/// The `invalid_rust_codeblock` lint detects Rust code blocks in
1852+
/// documentation examples that are invalid (e.g. empty, not parsable as
1853+
/// Rust code). This is a `rustdoc` only lint, see the documentation in the
1854+
/// [rustdoc book].
1855+
///
1856+
/// [rustdoc book]: ../../../rustdoc/lints.html#invalid_rust_codeblock
1857+
pub INVALID_RUST_CODEBLOCK,
1858+
Warn,
1859+
"codeblock could not be parsed as valid Rust or is empty"
1860+
}
1861+
18501862
declare_lint! {
18511863
/// The `missing_crate_level_docs` lint detects if documentation is
18521864
/// missing at the crate root. This is a `rustdoc` only lint, see the
@@ -2803,6 +2815,7 @@ declare_lint_pass! {
28032815
BROKEN_INTRA_DOC_LINKS,
28042816
PRIVATE_INTRA_DOC_LINKS,
28052817
INVALID_CODEBLOCK_ATTRIBUTES,
2818+
INVALID_RUST_CODEBLOCK,
28062819
MISSING_CRATE_LEVEL_DOCS,
28072820
MISSING_DOC_CODE_EXAMPLES,
28082821
INVALID_HTML_TAGS,

src/doc/rustdoc/src/lints.md

+35
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,41 @@ warning: unclosed HTML tag `h1`
285285
286286
warning: 2 warnings emitted
287287
```
288+
289+
## invalid_rust_codeblock
290+
291+
This lint **warns by default**. It detects Rust code blocks in documentation
292+
examples that are invalid (e.g. empty, not parsable as Rust). For example:
293+
294+
```rust
295+
/// Empty code block, with and without Rust:
296+
///
297+
/// ```rust
298+
/// ```
299+
///
300+
/// Unclosed code block:
301+
///
302+
/// ```rust
303+
fn main() {}
304+
```
305+
306+
Which will give:
307+
308+
```text
309+
warning: Rust code block is empty
310+
--> src/lib.rs:3:5
311+
|
312+
3 | /// ```rust
313+
| _____^
314+
4 | | /// ```
315+
| |_______^
316+
317+
warning: Rust code block is empty
318+
--> src/lib.rs:8:5
319+
|
320+
8 | /// ```rust
321+
| ^^^^^^^
322+
```
288323

289324
## non_autolinks
290325

src/librustdoc/core.rs

+2
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ crate fn run_core(
322322
let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
323323
let no_crate_level_docs = rustc_lint::builtin::MISSING_CRATE_LEVEL_DOCS.name;
324324
let invalid_codeblock_attributes_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTES.name;
325+
let invalid_rust_codeblock = rustc_lint::builtin::INVALID_RUST_CODEBLOCK.name;
325326
let invalid_html_tags = rustc_lint::builtin::INVALID_HTML_TAGS.name;
326327
let renamed_and_removed_lints = rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name;
327328
let non_autolinks = rustc_lint::builtin::NON_AUTOLINKS.name;
@@ -337,6 +338,7 @@ crate fn run_core(
337338
private_doc_tests.to_owned(),
338339
no_crate_level_docs.to_owned(),
339340
invalid_codeblock_attributes_name.to_owned(),
341+
invalid_rust_codeblock.to_owned(),
340342
invalid_html_tags.to_owned(),
341343
renamed_and_removed_lints.to_owned(),
342344
unknown_lints.to_owned(),

src/librustdoc/passes/check_code_block_syntax.rs

+49-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_data_structures::sync::{Lock, Lrc};
22
use rustc_errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
3+
use rustc_middle::lint::LintDiagnosticBuilder;
34
use rustc_parse::parse_stream_from_source_str;
5+
use rustc_session::lint;
46
use rustc_session::parse::ParseSess;
57
use rustc_span::source_map::{FilePathMapping, SourceMap};
68
use rustc_span::{FileName, InnerSpan};
@@ -47,51 +49,76 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
4749
.unwrap_or(false);
4850
let buffer = buffer.borrow();
4951

50-
if buffer.has_errors || is_empty {
51-
let mut diag = if let Some(sp) =
52-
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
53-
{
54-
let warning_message = if buffer.has_errors {
52+
if !(buffer.has_errors || is_empty) {
53+
// No errors in a non-empty program.
54+
return;
55+
}
56+
57+
let local_id = match item.def_id.as_local() {
58+
Some(id) => id,
59+
// We don't need to check the syntax for other crates so returning
60+
// without doing anything should not be a problem.
61+
None => return,
62+
};
63+
64+
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
65+
let mark_with_text = code_block.syntax.is_none() && code_block.is_fenced;
66+
67+
// The span and whether it is precise or not.
68+
let (sp, precise_span) = match super::source_span_for_markdown_range(
69+
self.cx,
70+
&dox,
71+
&code_block.range,
72+
&item.attrs,
73+
) {
74+
Some(sp) => (sp, true),
75+
None => (super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()), false),
76+
};
77+
78+
// lambda that will use the lint to start a new diagnostic and add
79+
// a suggestion to it when needed.
80+
let diag_builder = |lint: LintDiagnosticBuilder<'_>| {
81+
let mut diag = if precise_span {
82+
let msg = if buffer.has_errors {
5583
"could not parse code block as Rust code"
5684
} else {
5785
"Rust code block is empty"
5886
};
5987

60-
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
61-
62-
if code_block.syntax.is_none() && code_block.is_fenced {
63-
let sp = sp.from_inner(InnerSpan::new(0, 3));
88+
let mut diag = lint.build(msg);
89+
if mark_with_text {
6490
diag.span_suggestion(
65-
sp,
91+
sp.from_inner(InnerSpan::new(0, 3)),
6692
"mark blocks that do not contain Rust code as text",
6793
String::from("```text"),
6894
Applicability::MachineApplicable,
6995
);
7096
}
71-
7297
diag
7398
} else {
74-
// We couldn't calculate the span of the markdown block that had the error, so our
75-
// diagnostics are going to be a bit lacking.
76-
let mut diag = self.cx.sess().struct_span_warn(
77-
super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
78-
"doc comment contains an invalid Rust code block",
79-
);
80-
81-
if code_block.syntax.is_none() && code_block.is_fenced {
99+
let mut diag = lint.build("doc comment contains an invalid Rust code block");
100+
if mark_with_text {
82101
diag.help("mark blocks that do not contain Rust code as text: ```text");
83102
}
84103

85104
diag
86105
};
87-
88106
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
89107
for message in buffer.messages.iter() {
90108
diag.note(&message);
91109
}
92-
93110
diag.emit();
94-
}
111+
};
112+
113+
// Finally build and emit the completed diagnostic.
114+
// All points of divergence have been handled earlier so this can be
115+
// done the same way whether the span is precise or not.
116+
self.cx.tcx.struct_span_lint_hir(
117+
lint::builtin::INVALID_RUST_CODEBLOCK,
118+
hir_id,
119+
sp,
120+
diag_builder,
121+
);
95122
}
96123
}
97124

src/test/rustdoc-ui/invalid-syntax.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | | /// \__________pkt->size___________/ \_result->size_/ \__pkt->si
77
LL | | /// ```
88
| |_______^
99
|
10+
= note: `#[warn(invalid_rust_codeblock)]` on by default
1011
= note: error from rustc: unknown start of token: \
1112
= note: error from rustc: unknown start of token: \
1213
= note: error from rustc: unknown start of token: \

0 commit comments

Comments
 (0)