Skip to content

Commit 7e4bf4b

Browse files
committed
Remove --display-doctest-warnings
This can be replicated in full with other existing features, there's no need to have a separate option for it. This also fixes a bug where `--test-args=--show-output` had no effect, and updates the documentation.
1 parent 454cc5f commit 7e4bf4b

File tree

8 files changed

+68
-74
lines changed

8 files changed

+68
-74
lines changed

src/doc/rustdoc/src/documentation-tests.md

+10
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ conversion, so type inference fails because the type is not unique. Please note
261261
that you must write the `(())` in one sequence without intermediate whitespace
262262
so that `rustdoc` understands you want an implicit `Result`-returning function.
263263

264+
## Showing warnings in doctests
265+
266+
You can show warnings in doctests by running `rustdoc --test --test-args=--show-output`
267+
(or, if you're using cargo, `cargo test --doc -- --show-output`).
268+
By default, this will still hide `unused` warnings, since so many examples use private functions;
269+
you can add `#![warn(unused)]` to the top of your example if you want to see unused variables or dead code warnings.
270+
You can also use [`#![doc(test(attr(warn(unused))))]`][test-attr] in the crate root to enable warnings globally.
271+
272+
[test-attr]: ./the-doc-attribute.md#testattr
273+
264274
## Documenting macros
265275

266276
Here’s an example of documenting a macro:

src/doc/rustdoc/src/unstable-features.md

-16
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,6 @@ all these files are linked from every page, changing where they are can be cumbe
257257
specially cache them. This flag will rename all these files in the output to include the suffix in
258258
the filename. For example, `light.css` would become `light-suf.css` with the above command.
259259

260-
### `--display-doctest-warnings`: display warnings when documenting or running documentation tests
261-
262-
Using this flag looks like this:
263-
264-
```bash
265-
$ rustdoc src/lib.rs -Z unstable-options --display-doctest-warnings
266-
$ rustdoc --test src/lib.rs -Z unstable-options --display-doctest-warnings
267-
```
268-
269-
The intent behind this flag is to allow the user to see warnings that occur within their library or
270-
their documentation tests, which are usually suppressed. However, [due to a
271-
bug][issue-display-warnings], this flag doesn't 100% work as intended. See the linked issue for
272-
details.
273-
274-
[issue-display-warnings]: https://github.com/rust-lang/rust/issues/41574
275-
276260
### `--extern-html-root-url`: control how rustdoc links to non-local crates
277261

278262
Using this flag looks like this:

src/librustdoc/config.rs

-6
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ crate struct Options {
136136
///
137137
/// Be aware: This option can come both from the CLI and from crate attributes!
138138
crate manual_passes: Vec<String>,
139-
/// Whether to display warnings during doc generation or while gathering doctests. By default,
140-
/// all non-rustdoc-specific lints are allowed when generating docs.
141-
crate display_doctest_warnings: bool,
142139
/// Whether to run the `calculate-doc-coverage` pass, which counts the number of public items
143140
/// with and without documentation.
144141
crate show_coverage: bool,
@@ -197,7 +194,6 @@ impl fmt::Debug for Options {
197194
.field("persist_doctests", &self.persist_doctests)
198195
.field("default_passes", &self.default_passes)
199196
.field("manual_passes", &self.manual_passes)
200-
.field("display_doctest_warnings", &self.display_doctest_warnings)
201197
.field("show_coverage", &self.show_coverage)
202198
.field("crate_version", &self.crate_version)
203199
.field("render_options", &self.render_options)
@@ -639,7 +635,6 @@ impl Options {
639635
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
640636
let playground_url = matches.opt_str("playground-url");
641637
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
642-
let display_doctest_warnings = matches.opt_present("display-doctest-warnings");
643638
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
644639
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
645640
let enable_minification = !matches.opt_present("disable-minification");
@@ -707,7 +702,6 @@ impl Options {
707702
test_args,
708703
default_passes,
709704
manual_passes,
710-
display_doctest_warnings,
711705
show_coverage,
712706
crate_version,
713707
test_run_directory,

src/librustdoc/doctest.rs

+9-22
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ use crate::passes::span_of_attrs;
3838
crate struct TestOptions {
3939
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
4040
crate no_crate_inject: bool,
41-
/// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
42-
/// the default `#![allow(unused)]`.
43-
crate display_doctest_warnings: bool,
4441
/// Additional crate-level attributes to add to doctests.
4542
crate attrs: Vec<String>,
4643
}
@@ -65,14 +62,16 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
6562
}
6663
});
6764

65+
debug!(?lint_opts);
66+
6867
let crate_types =
6968
if options.proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
7069

7170
let sessopts = config::Options {
7271
maybe_sysroot: options.maybe_sysroot.clone(),
7372
search_paths: options.libs.clone(),
7473
crate_types,
75-
lint_opts: if !options.display_doctest_warnings { lint_opts } else { vec![] },
74+
lint_opts,
7675
lint_cap: Some(options.lint_cap.unwrap_or(lint::Forbid)),
7776
cg: options.codegen_options.clone(),
7877
externs: options.externs.clone(),
@@ -106,7 +105,6 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
106105
};
107106

108107
let test_args = options.test_args.clone();
109-
let display_doctest_warnings = options.display_doctest_warnings;
110108
let nocapture = options.nocapture;
111109
let externs = options.externs.clone();
112110
let json_unused_externs = options.json_unused_externs;
@@ -118,8 +116,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
118116
let collector = global_ctxt.enter(|tcx| {
119117
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
120118

121-
let mut opts = scrape_test_config(crate_attrs);
122-
opts.display_doctest_warnings |= options.display_doctest_warnings;
119+
let opts = scrape_test_config(crate_attrs);
123120
let enable_per_target_ignores = options.enable_per_target_ignores;
124121
let mut collector = Collector::new(
125122
tcx.crate_name(LOCAL_CRATE),
@@ -165,7 +162,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
165162
Err(ErrorReported) => return Err(ErrorReported),
166163
};
167164

168-
run_tests(test_args, nocapture, display_doctest_warnings, tests);
165+
run_tests(test_args, nocapture, tests);
169166

170167
// Collect and warn about unused externs, but only if we've gotten
171168
// reports for each doctest
@@ -208,29 +205,19 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
208205
Ok(())
209206
}
210207

211-
crate fn run_tests(
212-
mut test_args: Vec<String>,
213-
nocapture: bool,
214-
display_doctest_warnings: bool,
215-
tests: Vec<test::TestDescAndFn>,
216-
) {
208+
crate fn run_tests(mut test_args: Vec<String>, nocapture: bool, tests: Vec<test::TestDescAndFn>) {
217209
test_args.insert(0, "rustdoctest".to_string());
218210
if nocapture {
219211
test_args.push("--nocapture".to_string());
220212
}
221-
test::test_main(
222-
&test_args,
223-
tests,
224-
Some(test::Options::new().display_output(display_doctest_warnings)),
225-
);
213+
test::test_main(&test_args, tests, None);
226214
}
227215

228216
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
229217
fn scrape_test_config(attrs: &[ast::Attribute]) -> TestOptions {
230218
use rustc_ast_pretty::pprust;
231219

232-
let mut opts =
233-
TestOptions { no_crate_inject: false, display_doctest_warnings: false, attrs: Vec::new() };
220+
let mut opts = TestOptions { no_crate_inject: false, attrs: Vec::new() };
234221

235222
let test_attrs: Vec<_> = attrs
236223
.iter()
@@ -510,7 +497,7 @@ crate fn make_test(
510497
let mut prog = String::new();
511498
let mut supports_color = false;
512499

513-
if opts.attrs.is_empty() && !opts.display_doctest_warnings {
500+
if opts.attrs.is_empty() {
514501
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
515502
// lints that are commonly triggered in doctests. The crate-level test attributes are
516503
// commonly used to make tests fail in case they trigger warnings, so having this there in

src/librustdoc/doctest/tests.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ assert_eq!(2+2, 4);
5252
fn make_test_no_crate_inject() {
5353
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
5454
// adding it anyway.
55-
let opts =
56-
TestOptions { no_crate_inject: true, display_doctest_warnings: false, attrs: vec![] };
55+
let opts = TestOptions { no_crate_inject: true, attrs: vec![] };
5756
let input = "use asdf::qwop;
5857
assert_eq!(2+2, 4);";
5958
let expected = "#![allow(unused)]
@@ -215,20 +214,6 @@ assert_eq!(2+2, 4);"
215214
assert_eq!((output, len), (expected, 1));
216215
}
217216

218-
#[test]
219-
fn make_test_display_doctest_warnings() {
220-
// If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
221-
let mut opts = TestOptions::default();
222-
opts.display_doctest_warnings = true;
223-
let input = "assert_eq!(2+2, 4);";
224-
let expected = "fn main() {
225-
assert_eq!(2+2, 4);
226-
}"
227-
.to_string();
228-
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
229-
assert_eq!((output, len), (expected, 1));
230-
}
231-
232217
#[test]
233218
fn make_test_issues_21299_33731() {
234219
let opts = TestOptions::default();

src/librustdoc/markdown.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ crate fn test(options: Options) -> Result<(), String> {
131131
.map_err(|err| format!("{}: {}", options.input.display(), err))?;
132132
let mut opts = TestOptions::default();
133133
opts.no_crate_inject = true;
134-
opts.display_doctest_warnings = options.display_doctest_warnings;
135134
let mut collector = Collector::new(
136135
Symbol::intern(&options.input.display().to_string()),
137136
options.clone(),
@@ -146,11 +145,6 @@ crate fn test(options: Options) -> Result<(), String> {
146145

147146
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
148147

149-
crate::doctest::run_tests(
150-
options.test_args,
151-
options.nocapture,
152-
options.display_doctest_warnings,
153-
collector.tests,
154-
);
148+
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
155149
Ok(())
156150
}

src/test/rustdoc-ui/display-output.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
// Test that `--show-output` has an effect and `allow(unused)` can be overriden.
2+
13
// check-pass
2-
// compile-flags:-Zunstable-options --display-doctest-warnings --test
4+
// edition:2018
5+
// compile-flags:--test --test-args=--show-output
36
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
47
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
58

69
/// ```
10+
/// #![warn(unused)]
711
/// let x = 12;
12+
///
13+
/// fn foo(x: &std::fmt::Display) {}
814
/// ```
915
pub fn foo() {}
+40-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
11

22
running 1 test
3-
test $DIR/display-output.rs - foo (line 6) ... ok
3+
test $DIR/display-output.rs - foo (line 9) ... ok
44

55
successes:
66

7-
---- $DIR/display-output.rs - foo (line 6) stdout ----
7+
---- $DIR/display-output.rs - foo (line 9) stdout ----
8+
warning: trait objects without an explicit `dyn` are deprecated
9+
--> $DIR/display-output.rs:13:12
10+
|
11+
LL | fn foo(x: &std::fmt::Display) {}
12+
| ^^^^^^^^^^^^^^^^^ help: use `dyn`: `dyn std::fmt::Display`
13+
|
14+
= note: `#[warn(bare_trait_objects)]` on by default
15+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
16+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
17+
818
warning: unused variable: `x`
9-
--> $DIR/display-output.rs:7:5
19+
--> $DIR/display-output.rs:11:5
1020
|
1121
LL | let x = 12;
1222
| ^ help: if this is intentional, prefix it with an underscore: `_x`
1323
|
14-
= note: `#[warn(unused_variables)]` on by default
24+
note: the lint level is defined here
25+
--> $DIR/display-output.rs:9:9
26+
|
27+
LL | #![warn(unused)]
28+
| ^^^^^^
29+
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
30+
31+
warning: unused variable: `x`
32+
--> $DIR/display-output.rs:13:8
33+
|
34+
LL | fn foo(x: &std::fmt::Display) {}
35+
| ^ help: if this is intentional, prefix it with an underscore: `_x`
36+
37+
warning: function is never used: `foo`
38+
--> $DIR/display-output.rs:13:4
39+
|
40+
LL | fn foo(x: &std::fmt::Display) {}
41+
| ^^^
42+
|
43+
note: the lint level is defined here
44+
--> $DIR/display-output.rs:9:9
45+
|
46+
LL | #![warn(unused)]
47+
| ^^^^^^
48+
= note: `#[warn(dead_code)]` implied by `#[warn(unused)]`
1549

16-
warning: 1 warning emitted
50+
warning: 4 warnings emitted
1751

1852

1953

2054
successes:
21-
$DIR/display-output.rs - foo (line 6)
55+
$DIR/display-output.rs - foo (line 9)
2256

2357
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
2458

0 commit comments

Comments
 (0)