Skip to content

Commit 804ca1d

Browse files
Rollup merge of #78984 - GuillaumeGomez:rustdoc-check-option, r=jyn514
Rustdoc check option The ultimate goal behind this option would be to have `rustdoc --check` being run when you use `cargo check` as a second step. r? `@jyn514`
2 parents 0b7a793 + a06fd1f commit 804ca1d

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

src/librustdoc/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ pub struct Options {
145145
pub render_options: RenderOptions,
146146
/// Output format rendering (used only for "show-coverage" option for the moment)
147147
pub output_format: Option<OutputFormat>,
148+
/// If this option is set to `true`, rustdoc will only run checks and not generate
149+
/// documentation.
150+
pub run_check: bool,
148151
}
149152

150153
impl fmt::Debug for Options {
@@ -185,6 +188,7 @@ impl fmt::Debug for Options {
185188
.field("runtool", &self.runtool)
186189
.field("runtool_args", &self.runtool_args)
187190
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
191+
.field("run_check", &self.run_check)
188192
.finish()
189193
}
190194
}
@@ -581,6 +585,7 @@ impl Options {
581585
let enable_per_target_ignores = matches.opt_present("enable-per-target-ignores");
582586
let document_private = matches.opt_present("document-private-items");
583587
let document_hidden = matches.opt_present("document-hidden-items");
588+
let run_check = matches.opt_present("check");
584589

585590
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
586591

@@ -616,6 +621,7 @@ impl Options {
616621
runtool_args,
617622
enable_per_target_ignores,
618623
test_builder,
624+
run_check,
619625
render_options: RenderOptions {
620626
output,
621627
external_html,

src/librustdoc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ fn opts() -> Vec<RustcOptGroup> {
423423
"specified the rustc-like binary to use as the test builder",
424424
)
425425
}),
426+
unstable("check", |o| o.optflag("", "check", "Run rustdoc checks")),
426427
]
427428
}
428429

@@ -515,6 +516,7 @@ fn main_options(options: config::Options) -> MainResult {
515516
// but we can't crates the Handler ahead of time because it's not Send
516517
let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
517518
let show_coverage = options.show_coverage;
519+
let run_check = options.run_check;
518520

519521
// First, parse the crate and extract all relevant information.
520522
info!("starting to run rustc");
@@ -540,6 +542,9 @@ fn main_options(options: config::Options) -> MainResult {
540542
// if we ran coverage, bail early, we don't need to also generate docs at this point
541543
// (also we didn't load in any of the useful passes)
542544
return Ok(());
545+
} else if run_check {
546+
// Since we're in "check" mode, no need to generate anything beyond this point.
547+
return Ok(());
543548
}
544549

545550
info!("going to format");

src/test/rustdoc-ui/check-fail.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-flags: -Z unstable-options --check
2+
3+
#![deny(missing_docs)]
4+
#![deny(rustdoc)]
5+
6+
//! ```rust,testharness
7+
//~^ ERROR
8+
//! let x = 12;
9+
//! ```
10+
11+
pub fn foo() {}
12+
//~^ ERROR
13+
//~^^ ERROR
14+
15+
/// hello
16+
//~^ ERROR
17+
///
18+
/// ```rust,testharness
19+
/// let x = 12;
20+
/// ```
21+
pub fn bar() {}

src/test/rustdoc-ui/check-fail.stderr

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error: missing documentation for a function
2+
--> $DIR/check-fail.rs:11:1
3+
|
4+
LL | pub fn foo() {}
5+
| ^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/check-fail.rs:3:9
9+
|
10+
LL | #![deny(missing_docs)]
11+
| ^^^^^^^^^^^^
12+
13+
error: missing code example in this documentation
14+
--> $DIR/check-fail.rs:11:1
15+
|
16+
LL | pub fn foo() {}
17+
| ^^^^^^^^^^^^^^^
18+
|
19+
note: the lint level is defined here
20+
--> $DIR/check-fail.rs:4:9
21+
|
22+
LL | #![deny(rustdoc)]
23+
| ^^^^^^^
24+
= note: `#[deny(missing_doc_code_examples)]` implied by `#[deny(rustdoc)]`
25+
26+
error: unknown attribute `testharness`. Did you mean `test_harness`?
27+
--> $DIR/check-fail.rs:6:1
28+
|
29+
LL | / //! ```rust,testharness
30+
LL | |
31+
LL | | //! let x = 12;
32+
LL | | //! ```
33+
| |_______^
34+
|
35+
note: the lint level is defined here
36+
--> $DIR/check-fail.rs:4:9
37+
|
38+
LL | #![deny(rustdoc)]
39+
| ^^^^^^^
40+
= note: `#[deny(invalid_codeblock_attributes)]` implied by `#[deny(rustdoc)]`
41+
= help: the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
42+
43+
error: unknown attribute `testharness`. Did you mean `test_harness`?
44+
--> $DIR/check-fail.rs:15:1
45+
|
46+
LL | / /// hello
47+
LL | |
48+
LL | | ///
49+
LL | | /// ```rust,testharness
50+
LL | | /// let x = 12;
51+
LL | | /// ```
52+
| |_______^
53+
|
54+
= help: the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
55+
56+
error: aborting due to 4 previous errors
57+

src/test/rustdoc-ui/check.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
// compile-flags: -Z unstable-options --check
3+
4+
#![warn(missing_docs)]
5+
//~^ WARN
6+
//~^^ WARN
7+
#![warn(rustdoc)]
8+
9+
pub fn foo() {}
10+
//~^ WARN
11+
//~^^ WARN

src/test/rustdoc-ui/check.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
warning: missing documentation for the crate
2+
--> $DIR/check.rs:4:1
3+
|
4+
LL | / #![warn(missing_docs)]
5+
LL | |
6+
LL | |
7+
LL | | #![warn(rustdoc)]
8+
LL | |
9+
LL | | pub fn foo() {}
10+
| |_______________^
11+
|
12+
note: the lint level is defined here
13+
--> $DIR/check.rs:4:9
14+
|
15+
LL | #![warn(missing_docs)]
16+
| ^^^^^^^^^^^^
17+
18+
warning: missing documentation for a function
19+
--> $DIR/check.rs:9:1
20+
|
21+
LL | pub fn foo() {}
22+
| ^^^^^^^^^^^^
23+
24+
warning: missing code example in this documentation
25+
--> $DIR/check.rs:4:1
26+
|
27+
LL | / #![warn(missing_docs)]
28+
LL | |
29+
LL | |
30+
LL | | #![warn(rustdoc)]
31+
LL | |
32+
LL | | pub fn foo() {}
33+
| |_______________^
34+
|
35+
note: the lint level is defined here
36+
--> $DIR/check.rs:7:9
37+
|
38+
LL | #![warn(rustdoc)]
39+
| ^^^^^^^
40+
= note: `#[warn(missing_doc_code_examples)]` implied by `#[warn(rustdoc)]`
41+
42+
warning: missing code example in this documentation
43+
--> $DIR/check.rs:9:1
44+
|
45+
LL | pub fn foo() {}
46+
| ^^^^^^^^^^^^^^^
47+
48+
warning: 4 warnings emitted
49+

src/test/rustdoc/check.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-flags: -Z unstable-options --check
2+
3+
// @!has check/fn.foo.html
4+
// @!has check/index.html
5+
pub fn foo() {}

0 commit comments

Comments
 (0)