Skip to content

rustdoc: add --deny-doctest-warnings option #21316

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ endif
ifeq ($(2),$$(CFG_BUILD))
$$(call TEST_OK_FILE,$(1),$(2),$(3),doc-$(4)): $$(DOCTESTDEP_$(1)_$(2)_$(3)_$(4))
@$$(call E, run doc-$(4) [$(2)])
$$(Q)$$(RUSTDOC_$(1)_T_$(2)_H_$(3)) --cfg dox --test $$< \
$$(Q)$$(RUSTDOC_$(1)_T_$(2)_H_$(3)) --cfg dox --test --deny-doctest-warnings $$< \
--test-args "$$(TESTARGS)" && touch $$@
else
$$(call TEST_OK_FILE,$(1),$(2),$(3),doc-$(4)):
Expand Down Expand Up @@ -854,7 +854,7 @@ ifeq ($(2),$$(CFG_BUILD))
$$(call TEST_OK_FILE,$(1),$(2),$(3),doc-crate-$(4)): $$(CRATEDOCTESTDEP_$(1)_$(2)_$(3)_$(4))
@$$(call E, run doc-crate-$(4) [$(2)])
$$(Q)CFG_LLVM_LINKAGE_FILE=$$(LLVM_LINKAGE_PATH_$(3)) \
$$(RUSTDOC_$(1)_T_$(2)_H_$(3)) --test --cfg dox \
$$(RUSTDOC_$(1)_T_$(2)_H_$(3)) --test --deny-doctest-warnings --cfg dox \
$$(CRATEFILE_$(4)) --test-args "$$(TESTARGS)" && touch $$@
else
$$(call TEST_OK_FILE,$(1),$(2),$(3),doc-crate-$(4)):
Expand Down
4 changes: 3 additions & 1 deletion src/libcore/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//!
//! ```
//! # #![feature(unboxed_closures)]
//! #![allow(deprecated)]
//!
//! use std::finally::Finally;
//!
Expand Down Expand Up @@ -70,9 +71,10 @@ impl<T, F> Finally<T> for F where F: FnMut() -> T {
/// # Example
///
/// ```
/// #![allow(deprecated)]
/// use std::finally::try_finally;
///
/// struct State<'a> { buffer: &'a mut [u8], len: uint }
/// struct State<'a> { buffer: &'a mut [u8], len: usize }
/// # let mut buf = [];
/// let mut state = State { buffer: &mut buf, len: 0 };
/// try_finally(
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
//! # Examples
//!
//! ```rust
//! use std::hash::{hash, Hash, SipHasher};
//! use std::hash::{hash, SipHasher};
//!
//! #[derive(Hash)]
//! struct Person {
//! id: uint,
//! id: i32,
//! name: String,
//! phone: u64,
//! }
Expand All @@ -38,7 +38,7 @@
//! use std::hash::{hash, Hash, Hasher, Writer, SipHasher};
//!
//! struct Person {
//! id: uint,
//! id: i32,
//! name: String,
//! phone: u64,
//! }
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
stripped_filtered_line(l).unwrap_or(l)
}).collect::<Vec<&str>>().connect("\n");
let krate = krate.as_ref().map(|s| s.as_slice());
let test = test::maketest(test.as_slice(), krate, false, false);
let test = test::maketest(test.as_slice(), krate, false, false, false);
s.push_str(format!("<span class='rusttest'>{}</span>",
Escape(test.as_slice())).as_slice());
});
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ pub fn opts() -> Vec<getopts::OptGroup> {
"FILES"),
optopt("", "markdown-playground-url",
"URL to send code snippets to", "URL"),
optflag("", "markdown-no-toc", "don't include table of contents")
optflag("", "markdown-no-toc", "don't include table of contents"),
optflag("", "deny-doctest-warnings", "fail to compile on warnings in doc tests")
)
}

Expand Down Expand Up @@ -235,13 +236,14 @@ pub fn main_args(args: &[String]) -> int {
None => return 3
};
let crate_name = matches.opt_str("crate-name");
let deny_warnings = matches.opt_present("deny-doctest-warnings");

match (should_test, markdown_input) {
(true, true) => {
return markdown::test(input, libs, externs, test_args)
}
(true, false) => {
return test::run(input, cfgs, libs, externs, test_args, crate_name)
return test::run(input, cfgs, libs, externs, test_args, crate_name, deny_warnings)
}
(false, true) => return markdown::render(input, output.unwrap_or(Path::new("doc")),
&matches, &external_html,
Expand Down
20 changes: 15 additions & 5 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub fn run(input: &str,
libs: SearchPaths,
externs: core::Externs,
mut test_args: Vec<String>,
crate_name: Option<String>)
crate_name: Option<String>,
deny_warnings: bool)
-> int {
let input_path = Path::new(input);
let input = config::Input::File(input_path.clone());
Expand Down Expand Up @@ -101,6 +102,7 @@ pub fn run(input: &str,
libs,
externs,
false);
collector.deny_warnings = deny_warnings;
collector.fold_crate(krate);

test_args.insert(0, "rustdoctest".to_string());
Expand All @@ -112,10 +114,10 @@ pub fn run(input: &str,

fn runtest(test: &str, cratename: &str, libs: SearchPaths,
externs: core::Externs,
should_fail: bool, no_run: bool, as_test_harness: bool) {
should_fail: bool, no_run: bool, as_test_harness: bool, deny_warnings: bool) {
// the test harness wants its own `main` & top level functions, so
// never wrap the test in `fn main() { ... }`
let test = maketest(test, Some(cratename), true, as_test_harness);
let test = maketest(test, Some(cratename), true, as_test_harness, deny_warnings);
let input = config::Input::Str(test.to_string());

let sessopts = config::Options {
Expand Down Expand Up @@ -214,13 +216,17 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
}
}

pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main: bool) -> String {
pub fn maketest(s: &str, cratename: Option<&str>,
lints: bool, dont_insert_main: bool, deny_warnings: bool) -> String {
let mut prog = String::new();
if lints {
prog.push_str(r"
#![allow(unused_variables, unused_assignments, unused_mut, unused_attributes, dead_code)]
");
}
if deny_warnings {
prog.push_str("#![deny(warnings)]\n");
}

// Don't inject `extern crate std` because it's already injected by the
// compiler.
Expand Down Expand Up @@ -255,6 +261,7 @@ pub struct Collector {
use_headers: bool,
current_header: Option<String>,
cratename: String,
deny_warnings: bool,
}

impl Collector {
Expand All @@ -269,6 +276,7 @@ impl Collector {
use_headers: use_headers,
current_header: None,
cratename: cratename,
deny_warnings: false,
}
}

Expand All @@ -284,6 +292,7 @@ impl Collector {
let libs = self.libs.clone();
let externs = self.externs.clone();
let cratename = self.cratename.to_string();
let deny_warnings = self.deny_warnings;
debug!("Creating test {}: {}", name, test);
self.tests.push(testing::TestDescAndFn {
desc: testing::TestDesc {
Expand All @@ -298,7 +307,8 @@ impl Collector {
externs,
should_fail,
no_run,
as_test_harness);
as_test_harness,
deny_warnings);
}))
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use sys_common::mutex as sys;
/// let data = Arc::new(Mutex::new(0));
///
/// let (tx, rx) = channel();
/// for _ in range(0u, 10) {
/// for _ in range(0us, 10) {
/// let (data, tx) = (data.clone(), tx.clone());
/// Thread::spawn(move || {
/// // The shared static can only be accessed once the lock is held.
Expand All @@ -87,7 +87,7 @@ use sys_common::mutex as sys;
/// use std::sync::{Arc, Mutex};
/// use std::thread::Thread;
///
/// let lock = Arc::new(Mutex::new(0u));
/// let lock = Arc::new(Mutex::new(0us));
/// let lock2 = lock.clone();
///
/// let _ = Thread::scoped(move || -> () {
Expand All @@ -104,7 +104,7 @@ use sys_common::mutex as sys;
/// // pattern matched on to return the underlying guard on both branches.
/// let mut guard = match lock.lock() {
/// Ok(guard) => guard,
/// Err(poisoned) => poisoned.into_guard(),
/// Err(poisoned) => poisoned.into_inner(),
/// };
///
/// *guard += 1;
Expand Down