Skip to content

Commit 07d11cf

Browse files
Rollup merge of #84587 - jyn514:rustdoc-lint-block, r=CraftSpider
rustdoc: Make "rust code block is empty" and "could not parse code block" warnings a lint (`INVALID_RUST_CODEBLOCKS`) Fixes #79792. This already went through FCP in #79816, so it only needs final review. This is mostly a rebase of #79816 - thank you ``@poliorcetics`` for doing most of the work!
2 parents 25a277f + 587c504 commit 07d11cf

File tree

10 files changed

+143
-55
lines changed

10 files changed

+143
-55
lines changed

compiler/rustc_mir/src/borrow_check/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
12411241
/// it. However, it works pretty well in practice. In particular,
12421242
/// this is needed to deal with projection outlives bounds like
12431243
///
1244-
/// ```ignore (internal compiler representation so lifetime syntax is invalid)
1244+
/// ```text
12451245
/// <T as Foo<'0>>::Item: '1
12461246
/// ```
12471247
///

compiler/rustc_trait_selection/src/opaque_types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub struct OpaqueTypeDecl<'tcx> {
4646
/// type Foo = impl Baz;
4747
/// fn bar() -> Foo {
4848
/// // ^^^ This is the span we are looking for!
49+
/// }
4950
/// ```
5051
///
5152
/// In cases where the fn returns `(impl Trait, impl Trait)` or

compiler/rustc_typeck/src/check/upvar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
323323
///
324324
/// InferBorrowKind results in a structure like this:
325325
///
326-
/// ```
326+
/// ```text
327327
/// {
328328
/// Place(base: hir_id_s, projections: [], ....) -> {
329329
/// capture_kind_expr: hir_id_L5,
@@ -348,7 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
348348
/// ```
349349
///
350350
/// After the min capture analysis, we get:
351-
/// ```
351+
/// ```text
352352
/// {
353353
/// hir_id_s -> [
354354
/// Place(base: hir_id_s, projections: [], ....) -> {

src/doc/rustdoc/src/lints.md

+44
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,50 @@ warning: unclosed HTML tag `h1`
294294
warning: 2 warnings emitted
295295
```
296296

297+
## invalid_rust_codeblocks
298+
299+
This lint **warns by default**. It detects Rust code blocks in documentation
300+
examples that are invalid (e.g. empty, not parsable as Rust). For example:
301+
302+
```rust
303+
/// Empty code blocks (with and without the `rust` marker):
304+
///
305+
/// ```rust
306+
/// ```
307+
///
308+
/// Invalid syntax in code blocks:
309+
///
310+
/// ```rust
311+
/// '<
312+
/// ```
313+
pub fn foo() {}
314+
```
315+
316+
Which will give:
317+
318+
```text
319+
warning: Rust code block is empty
320+
--> lint.rs:3:5
321+
|
322+
3 | /// ```rust
323+
| _____^
324+
4 | | /// ```
325+
| |_______^
326+
|
327+
= note: `#[warn(rustdoc::invalid_rust_codeblocks)]` on by default
328+
329+
warning: could not parse code block as Rust code
330+
--> lint.rs:8:5
331+
|
332+
8 | /// ```rust
333+
| _____^
334+
9 | | /// '<
335+
10 | | /// ```
336+
| |_______^
337+
|
338+
= note: error from rustc: unterminated character literal
339+
```
340+
297341
## bare_urls
298342

299343
This lint is **warn-by-default**. It detects URLs which are not links.

src/librustdoc/lint.rs

+13
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,26 @@ declare_rustdoc_lint! {
157157
"detects URLs that are not hyperlinks"
158158
}
159159

160+
declare_rustdoc_lint! {
161+
/// The `invalid_rust_codeblocks` lint detects Rust code blocks in
162+
/// documentation examples that are invalid (e.g. empty, not parsable as
163+
/// Rust code). This is a `rustdoc` only lint, see the documentation in the
164+
/// [rustdoc book].
165+
///
166+
/// [rustdoc book]: ../../../rustdoc/lints.html#invalid_rust_codeblocks
167+
INVALID_RUST_CODEBLOCKS,
168+
Warn,
169+
"codeblock could not be parsed as valid Rust or is empty"
170+
}
171+
160172
crate static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| {
161173
vec![
162174
BROKEN_INTRA_DOC_LINKS,
163175
PRIVATE_INTRA_DOC_LINKS,
164176
MISSING_DOC_CODE_EXAMPLES,
165177
PRIVATE_DOC_TESTS,
166178
INVALID_CODEBLOCK_ATTRIBUTES,
179+
INVALID_RUST_CODEBLOCKS,
167180
INVALID_HTML_TAGS,
168181
BARE_URLS,
169182
MISSING_CRATE_LEVEL_DOCS,

src/librustdoc/passes/check_code_block_syntax.rs

+69-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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;
45
use rustc_session::parse::ParseSess;
56
use rustc_span::source_map::{FilePathMapping, SourceMap};
@@ -47,63 +48,86 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
4748
.unwrap_or(false);
4849
let buffer = buffer.borrow();
4950

50-
if buffer.has_errors || is_empty {
51-
let mut diag = if let Some(sp) = super::source_span_for_markdown_range(
52-
self.cx.tcx,
53-
&dox,
54-
&code_block.range,
55-
&item.attrs,
56-
) {
57-
let (warning_message, suggest_using_text) = if buffer.has_errors {
58-
("could not parse code block as Rust code", true)
59-
} else {
60-
("Rust code block is empty", false)
61-
};
62-
63-
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
64-
65-
if code_block.syntax.is_none() && code_block.is_fenced {
66-
let sp = sp.from_inner(InnerSpan::new(0, 3));
67-
diag.span_suggestion(
68-
sp,
69-
"mark blocks that do not contain Rust code as text",
70-
String::from("```text"),
71-
Applicability::MachineApplicable,
51+
if !buffer.has_errors && !is_empty {
52+
// No errors in a non-empty program.
53+
return;
54+
}
55+
56+
let local_id = match item.def_id.as_real().and_then(|x| x.as_local()) {
57+
Some(id) => id,
58+
// We don't need to check the syntax for other crates so returning
59+
// without doing anything should not be a problem.
60+
None => return,
61+
};
62+
63+
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
64+
let empty_block = code_block.syntax.is_none() && code_block.is_fenced;
65+
let is_ignore = code_block.is_ignore;
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.tcx,
70+
&dox,
71+
&code_block.range,
72+
&item.attrs,
73+
) {
74+
Some(sp) => (sp, true),
75+
None => (item.attr_span(self.cx.tcx), 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 explanation = if is_ignore {
82+
"`ignore` code blocks require valid Rust code for syntax highlighting; \
83+
mark blocks that do not contain Rust code as text"
84+
} else {
85+
"mark blocks that do not contain Rust code as text"
86+
};
87+
let msg = if buffer.has_errors {
88+
"could not parse code block as Rust code"
89+
} else {
90+
"Rust code block is empty"
91+
};
92+
let mut diag = lint.build(msg);
93+
94+
if precise_span {
95+
if is_ignore {
96+
// giving an accurate suggestion is hard because `ignore` might not have come first in the list.
97+
// just give a `help` instead.
98+
diag.span_help(
99+
sp.from_inner(InnerSpan::new(0, 3)),
100+
&format!("{}: ```text", explanation),
72101
);
73-
} else if suggest_using_text && code_block.is_ignore {
74-
let sp = sp.from_inner(InnerSpan::new(0, 3));
102+
} else if empty_block {
75103
diag.span_suggestion(
76-
sp,
77-
"`ignore` code blocks require valid Rust code for syntax highlighting. \
78-
Mark blocks that do not contain Rust code as text",
79-
String::from("```text,"),
104+
sp.from_inner(InnerSpan::new(0, 3)),
105+
explanation,
106+
String::from("```text"),
80107
Applicability::MachineApplicable,
81108
);
82109
}
83-
84-
diag
85-
} else {
86-
// We couldn't calculate the span of the markdown block that had the error, so our
87-
// diagnostics are going to be a bit lacking.
88-
let mut diag = self.cx.sess().struct_span_warn(
89-
item.attr_span(self.cx.tcx),
90-
"doc comment contains an invalid Rust code block",
91-
);
92-
93-
if code_block.syntax.is_none() && code_block.is_fenced {
94-
diag.help("mark blocks that do not contain Rust code as text: ```text");
95-
}
96-
97-
diag
98-
};
110+
} else if empty_block || is_ignore {
111+
diag.help(&format!("{}: ```text", explanation));
112+
}
99113

100114
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
101115
for message in buffer.messages.iter() {
102116
diag.note(&message);
103117
}
104118

105119
diag.emit();
106-
}
120+
};
121+
122+
// Finally build and emit the completed diagnostic.
123+
// All points of divergence have been handled earlier so this can be
124+
// done the same way whether the span is precise or not.
125+
self.cx.tcx.struct_span_lint_hir(
126+
crate::lint::INVALID_RUST_CODEBLOCKS,
127+
hir_id,
128+
sp,
129+
diag_builder,
130+
);
107131
}
108132
}
109133

src/test/rustdoc-ui/ignore-block-help.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
/// ```ignore (to-prevent-tidy-error)
44
/// let heart = '❤️';
55
/// ```
6-
//~^^^ WARN
6+
//~^^^ WARNING could not parse code block
7+
//~| NOTE on by default
8+
//~| NOTE character literal may only contain one codepoint
9+
//~| HELP `ignore` code blocks require valid Rust code
710
pub struct X;

src/test/rustdoc-ui/ignore-block-help.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ LL | | /// let heart = '❤️';
77
LL | | /// ```
88
| |_______^
99
|
10-
= note: error from rustc: character literal may only contain one codepoint
11-
help: `ignore` code blocks require valid Rust code for syntax highlighting. Mark blocks that do not contain Rust code as text
10+
= note: `#[warn(rustdoc::invalid_rust_codeblocks)]` on by default
11+
help: `ignore` code blocks require valid Rust code for syntax highlighting; mark blocks that do not contain Rust code as text: ```text
12+
--> $DIR/ignore-block-help.rs:3:5
1213
|
13-
LL | /// ```text,ignore (to-prevent-tidy-error)
14-
| ^^^^^^^^
14+
LL | /// ```ignore (to-prevent-tidy-error)
15+
| ^^^
16+
= note: error from rustc: character literal may only contain one codepoint
1517

1618
warning: 1 warning emitted
1719

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn blargh() {}
7171
/// \_
7272
#[doc = "```"]
7373
pub fn crazy_attrs() {}
74-
//~^^^^ WARNING doc comment contains an invalid Rust code block
74+
//~^^^^ WARNING could not parse code block
7575

7676
/// ```rust
7777
/// ```

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

+2-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(rustdoc::invalid_rust_codeblocks)]` 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: \
@@ -90,7 +91,7 @@ LL | | /// ```
9091
|
9192
= note: error from rustc: unknown start of token: \
9293

93-
warning: doc comment contains an invalid Rust code block
94+
warning: could not parse code block as Rust code
9495
--> $DIR/invalid-syntax.rs:70:1
9596
|
9697
LL | / #[doc = "```"]

0 commit comments

Comments
 (0)