Skip to content

Commit 11f1b9b

Browse files
committed
rustdoc: add --deny-doctest-warnings option
Collaboration with @rylev!
1 parent 89c4e37 commit 11f1b9b

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
199199
stripped_filtered_line(l).unwrap_or(l)
200200
}).collect::<Vec<&str>>().connect("\n");
201201
let krate = krate.as_ref().map(|s| s.as_slice());
202-
let test = test::maketest(test.as_slice(), krate, false, false);
202+
let test = test::maketest(test.as_slice(), krate, false, false, false);
203203
s.push_str(format!("<span class='rusttest'>{}</span>",
204204
Escape(test.as_slice())).as_slice());
205205
});

src/librustdoc/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ pub fn opts() -> Vec<getopts::OptGroup> {
156156
"FILES"),
157157
optopt("", "markdown-playground-url",
158158
"URL to send code snippets to", "URL"),
159-
optflag("", "markdown-no-toc", "don't include table of contents")
159+
optflag("", "markdown-no-toc", "don't include table of contents"),
160+
optflag("", "deny-doctest-warnings", "fail to compile on warnings in doc tests")
160161
)
161162
}
162163

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

239241
match (should_test, markdown_input) {
240242
(true, true) => {
241243
return markdown::test(input, libs, externs, test_args)
242244
}
243245
(true, false) => {
244-
return test::run(input, cfgs, libs, externs, test_args, crate_name)
246+
return test::run(input, cfgs, libs, externs, test_args, crate_name, deny_warnings)
245247
}
246248
(false, true) => return markdown::render(input, output.unwrap_or(Path::new("doc")),
247249
&matches, &external_html,

src/librustdoc/test.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub fn run(input: &str,
4343
libs: SearchPaths,
4444
externs: core::Externs,
4545
mut test_args: Vec<String>,
46-
crate_name: Option<String>)
46+
crate_name: Option<String>,
47+
deny_warnings: bool)
4748
-> int {
4849
let input_path = Path::new(input);
4950
let input = config::Input::File(input_path.clone());
@@ -101,6 +102,7 @@ pub fn run(input: &str,
101102
libs,
102103
externs,
103104
false);
105+
collector.deny_warnings = deny_warnings;
104106
collector.fold_crate(krate);
105107

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

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

121123
let sessopts = config::Options {
@@ -214,13 +216,17 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
214216
}
215217
}
216218

217-
pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main: bool) -> String {
219+
pub fn maketest(s: &str, cratename: Option<&str>,
220+
lints: bool, dont_insert_main: bool, deny_warnings: bool) -> String {
218221
let mut prog = String::new();
219222
if lints {
220223
prog.push_str(r"
221224
#![allow(unused_variables, unused_assignments, unused_mut, unused_attributes, dead_code)]
222225
");
223226
}
227+
if deny_warnings {
228+
prog.push_str("#![deny(warnings)]\n");
229+
}
224230

225231
// Don't inject `extern crate std` because it's already injected by the
226232
// compiler.
@@ -255,6 +261,7 @@ pub struct Collector {
255261
use_headers: bool,
256262
current_header: Option<String>,
257263
cratename: String,
264+
deny_warnings: bool,
258265
}
259266

260267
impl Collector {
@@ -269,6 +276,7 @@ impl Collector {
269276
use_headers: use_headers,
270277
current_header: None,
271278
cratename: cratename,
279+
deny_warnings: false,
272280
}
273281
}
274282

@@ -284,6 +292,7 @@ impl Collector {
284292
let libs = self.libs.clone();
285293
let externs = self.externs.clone();
286294
let cratename = self.cratename.to_string();
295+
let deny_warnings = self.deny_warnings;
287296
debug!("Creating test {}: {}", name, test);
288297
self.tests.push(testing::TestDescAndFn {
289298
desc: testing::TestDesc {
@@ -298,7 +307,8 @@ impl Collector {
298307
externs,
299308
should_fail,
300309
no_run,
301-
as_test_harness);
310+
as_test_harness,
311+
deny_warnings);
302312
}))
303313
});
304314
}

0 commit comments

Comments
 (0)