Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoctest: suppress the default allow(unused) under --display-warnings #49064

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,

let mut opts = TestOptions::default();
opts.no_crate_inject = true;
opts.display_warnings = display_warnings;
let mut collector = Collector::new(input.to_owned(), cfgs, libs, externs,
true, opts, maybe_sysroot, None,
Some(PathBuf::from(input)),
Expand Down
27 changes: 25 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ use html::markdown;

#[derive(Clone, Default)]
pub struct TestOptions {
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
pub no_crate_inject: bool,
/// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
/// the default `#![allow(unused)]`.
pub display_warnings: bool,
/// Additional crate-level attributes to add to doctests.
pub attrs: Vec<String>,
}

Expand Down Expand Up @@ -113,7 +118,8 @@ pub fn run(input_path: &Path,
let crate_name = crate_name.unwrap_or_else(|| {
::rustc_trans_utils::link::find_crate_name(None, &hir_forest.krate().attrs, &input)
});
let opts = scrape_test_config(hir_forest.krate());
let mut opts = scrape_test_config(hir_forest.krate());
opts.display_warnings |= display_warnings;
let mut collector = Collector::new(crate_name,
cfgs,
libs,
Expand Down Expand Up @@ -153,6 +159,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {

let mut opts = TestOptions {
no_crate_inject: false,
display_warnings: false,
attrs: Vec::new(),
};

Expand Down Expand Up @@ -357,7 +364,7 @@ pub fn make_test(s: &str,
let mut line_offset = 0;
let mut prog = String::new();

if opts.attrs.is_empty() {
if opts.attrs.is_empty() && !opts.display_warnings {
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
// lints that are commonly triggered in doctests. The crate-level test attributes are
// commonly used to make tests fail in case they trigger warnings, so having this there in
Expand Down Expand Up @@ -787,6 +794,7 @@ assert_eq!(2+2, 4);
//adding it anyway
let opts = TestOptions {
no_crate_inject: true,
display_warnings: false,
attrs: vec![],
};
let input =
Expand Down Expand Up @@ -957,4 +965,19 @@ assert_eq!(2+2, 4);".to_string();
let output = make_test(input, None, true, &opts);
assert_eq!(output, (expected.clone(), 1));
}

#[test]
fn make_test_display_warnings() {
//if the user is asking to display doctest warnings, suppress the default allow(unused)
let mut opts = TestOptions::default();
opts.display_warnings = true;
let input =
"assert_eq!(2+2, 4);";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this strange indent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since make_test doesn't add indentation to any of the code it parses, i moved everything to the far left margin so i wouldn't have to deal with other ways of keeping the indentation right. For these one-line examples it's not necessary but i wanted both the "before" and "after" to be on the same indent level to visually compare them more easily.

let expected =
"fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts);
assert_eq!(output, (expected.clone(), 1));
}
}