Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 05fbfde

Browse files
Clean up rustdoc make_test function code
1 parent 80eb5a8 commit 05fbfde

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) fn make_test(
2525
dont_insert_main: bool,
2626
opts: &GlobalTestOptions,
2727
edition: Edition,
28+
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
2829
test_id: Option<&str>,
2930
) -> (String, usize, bool) {
3031
let (crate_attrs, everything_else, crates) = partition_source(s, edition);
@@ -65,21 +66,22 @@ pub(crate) fn make_test(
6566

6667
// Don't inject `extern crate std` because it's already injected by the
6768
// compiler.
68-
if !already_has_extern_crate && !opts.no_crate_inject && crate_name != Some("std") {
69-
if let Some(crate_name) = crate_name {
70-
// Don't inject `extern crate` if the crate is never used.
71-
// NOTE: this is terribly inaccurate because it doesn't actually
72-
// parse the source, but only has false positives, not false
73-
// negatives.
74-
if s.contains(crate_name) {
75-
// rustdoc implicitly inserts an `extern crate` item for the own crate
76-
// which may be unused, so we need to allow the lint.
77-
prog.push_str("#[allow(unused_extern_crates)]\n");
78-
79-
prog.push_str(&format!("extern crate r#{crate_name};\n"));
80-
line_offset += 1;
81-
}
82-
}
69+
if !already_has_extern_crate &&
70+
!opts.no_crate_inject &&
71+
let Some(crate_name) = crate_name &&
72+
crate_name != "std" &&
73+
// Don't inject `extern crate` if the crate is never used.
74+
// NOTE: this is terribly inaccurate because it doesn't actually
75+
// parse the source, but only has false positives, not false
76+
// negatives.
77+
s.contains(crate_name)
78+
{
79+
// rustdoc implicitly inserts an `extern crate` item for the own crate
80+
// which may be unused, so we need to allow the lint.
81+
prog.push_str("#[allow(unused_extern_crates)]\n");
82+
83+
prog.push_str(&format!("extern crate r#{crate_name};\n"));
84+
line_offset += 1;
8385
}
8486

8587
// FIXME: This code cannot yet handle no_std test cases yet
@@ -234,22 +236,20 @@ fn check_for_main_and_extern_crate(
234236
(found_main, found_extern_crate, found_macro)
235237
})
236238
});
237-
let (already_has_main, already_has_extern_crate, found_macro) = result?;
239+
let (mut already_has_main, already_has_extern_crate, found_macro) = result?;
238240

239241
// If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't
240242
// see it. In that case, run the old text-based scan to see if they at least have a main
241243
// function written inside a macro invocation. See
242244
// https://github.com/rust-lang/rust/issues/56898
243-
let already_has_main = if found_macro && !already_has_main {
244-
source
245+
if found_macro && !already_has_main {
246+
already_has_main = source
245247
.lines()
246248
.map(|line| {
247249
let comment = line.find("//");
248250
if let Some(comment_begins) = comment { &line[0..comment_begins] } else { line }
249251
})
250-
.any(|code| code.contains("fn main"))
251-
} else {
252-
already_has_main
252+
.any(|code| code.contains("fn main"));
253253
};
254254

255255
Ok((already_has_main, already_has_extern_crate))

0 commit comments

Comments
 (0)