Skip to content

Commit 39ab57e

Browse files
committed
Rollup merge of #45764 - QuietMisdreavus:rustdoc-doctest-lints, r=GuillaumeGomez
rustdoc: add #[allow(unused)] to every doctest More information in #45750 - this is behavior that was documented but not actually implemented. I also reordered how outer attributes are applied to doctests. Previously, attributes from `#![doc(test(attr(...)))]` would be applied *after* attributes from within the test itself, meaning if a doctest tried to override lints that would be set crate-wide, it wouldn't work at all. This gives a better scope of how lints can be applied. Closes #45750
2 parents 93a7fd0 + ce2768a commit 39ab57e

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/librustdoc/test.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,23 @@ pub fn make_test(s: &str,
337337

338338
let mut prog = String::new();
339339

340-
// First push any outer attributes from the example, assuming they
341-
// are intended to be crate attributes.
342-
prog.push_str(&crate_attrs);
340+
if opts.attrs.is_empty() {
341+
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
342+
// lints that are commonly triggered in doctests. The crate-level test attributes are
343+
// commonly used to make tests fail in case they trigger warnings, so having this there in
344+
// that case may cause some tests to pass when they shouldn't have.
345+
prog.push_str("#![allow(unused)]\n");
346+
}
343347

344-
// Next, any attributes for other aspects such as lints.
348+
// Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
345349
for attr in &opts.attrs {
346350
prog.push_str(&format!("#![{}]\n", attr));
347351
}
348352

353+
// Now push any outer attributes from the example, assuming they
354+
// are intended to be crate attributes.
355+
prog.push_str(&crate_attrs);
356+
349357
// Don't inject `extern crate std` because it's already injected by the
350358
// compiler.
351359
if !s.contains("extern crate") && !opts.no_crate_inject && cratename != Some("std") {

src/test/rustdoc/playground-arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
pub fn dummy() {}
2222

2323
// ensure that `extern crate foo;` was inserted into code snips automatically:
24-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=extern%20crate%20foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D"]' "Run"
24+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0Aextern%20crate%20foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D"]' "Run"

src/test/rustdoc/playground.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
//! }
3535
//! ```
3636
37-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=fn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D%0A"]' "Run"
38-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=fn%20main()%20%7B%0Aprintln!(%22Hello%2C%20world!%22)%3B%0A%7D"]' "Run"
39-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Bfeature(something)%5D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D%0A&version=nightly"]' "Run"
37+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D%0A"]' "Run"
38+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aprintln!(%22Hello%2C%20world!%22)%3B%0A%7D"]' "Run"
39+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(something)%5D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D%0A&version=nightly"]' "Run"

0 commit comments

Comments
 (0)