Skip to content

Commit 5abc524

Browse files
rustdoc: add #[allow(unused)] to every doctest
also modify the order crate attributes are applied, to have a better order of how things can override lints, either per-crate or per-test
1 parent dcd343b commit 5abc524

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/librustdoc/test.rs

+12-4
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") {

0 commit comments

Comments
 (0)