Skip to content

Commit 218c9f4

Browse files
Clean up rustdoc make_test function code
1 parent 2805aed commit 218c9f4

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/librustdoc/doctest.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ pub(crate) fn make_test(
582582
dont_insert_main: bool,
583583
opts: &GlobalTestOptions,
584584
edition: Edition,
585+
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
585586
test_id: Option<&str>,
586587
) -> (String, usize, bool) {
587588
let (crate_attrs, everything_else, crates) = partition_source(s, edition);
@@ -703,7 +704,7 @@ pub(crate) fn make_test(
703704
(found_main, found_extern_crate, found_macro)
704705
})
705706
});
706-
let Ok((already_has_main, already_has_extern_crate, found_macro)) = result else {
707+
let Ok((mut already_has_main, already_has_extern_crate, found_macro)) = result else {
707708
// If the parser panicked due to a fatal error, pass the test code through unchanged.
708709
// The error will be reported during compilation.
709710
return (s.to_owned(), 0, false);
@@ -713,34 +714,34 @@ pub(crate) fn make_test(
713714
// see it. In that case, run the old text-based scan to see if they at least have a main
714715
// function written inside a macro invocation. See
715716
// https://github.com/rust-lang/rust/issues/56898
716-
let already_has_main = if found_macro && !already_has_main {
717-
s.lines()
717+
if found_macro && !already_has_main {
718+
already_has_main = s
719+
.lines()
718720
.map(|line| {
719721
let comment = line.find("//");
720722
if let Some(comment_begins) = comment { &line[0..comment_begins] } else { line }
721723
})
722-
.any(|code| code.contains("fn main"))
723-
} else {
724-
already_has_main
725-
};
724+
.any(|code| code.contains("fn main"));
725+
}
726726

727727
// Don't inject `extern crate std` because it's already injected by the
728728
// compiler.
729-
if !already_has_extern_crate && !opts.no_crate_inject && crate_name != Some("std") {
730-
if let Some(crate_name) = crate_name {
731-
// Don't inject `extern crate` if the crate is never used.
732-
// NOTE: this is terribly inaccurate because it doesn't actually
733-
// parse the source, but only has false positives, not false
734-
// negatives.
735-
if s.contains(crate_name) {
736-
// rustdoc implicitly inserts an `extern crate` item for the own crate
737-
// which may be unused, so we need to allow the lint.
738-
prog.push_str("#[allow(unused_extern_crates)]\n");
739-
740-
prog.push_str(&format!("extern crate r#{crate_name};\n"));
741-
line_offset += 1;
742-
}
743-
}
729+
if !already_has_extern_crate &&
730+
!opts.no_crate_inject &&
731+
let Some(crate_name) = crate_name &&
732+
crate_name != "std" &&
733+
// Don't inject `extern crate` if the crate is never used.
734+
// NOTE: this is terribly inaccurate because it doesn't actually
735+
// parse the source, but only has false positives, not false
736+
// negatives.
737+
s.contains(crate_name)
738+
{
739+
// rustdoc implicitly inserts an `extern crate` item for the own crate
740+
// which may be unused, so we need to allow the lint.
741+
prog.push_str("#[allow(unused_extern_crates)]\n");
742+
743+
prog.push_str(&format!("extern crate r#{crate_name};\n"));
744+
line_offset += 1;
744745
}
745746

746747
// FIXME: This code cannot yet handle no_std test cases yet

0 commit comments

Comments
 (0)