@@ -25,6 +25,7 @@ pub(crate) fn make_test(
25
25
dont_insert_main : bool ,
26
26
opts : & GlobalTestOptions ,
27
27
edition : Edition ,
28
+ // If `test_id` is `None`, it means we're generating code for a code example "run" link.
28
29
test_id : Option < & str > ,
29
30
) -> ( String , usize , bool ) {
30
31
let ( crate_attrs, everything_else, crates) = partition_source ( s, edition) ;
@@ -65,21 +66,22 @@ pub(crate) fn make_test(
65
66
66
67
// Don't inject `extern crate std` because it's already injected by the
67
68
// 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 ;
83
85
}
84
86
85
87
// FIXME: This code cannot yet handle no_std test cases yet
@@ -234,22 +236,20 @@ fn check_for_main_and_extern_crate(
234
236
( found_main, found_extern_crate, found_macro)
235
237
} )
236
238
} ) ;
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?;
238
240
239
241
// If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't
240
242
// see it. In that case, run the old text-based scan to see if they at least have a main
241
243
// function written inside a macro invocation. See
242
244
// 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
245
247
. lines ( )
246
248
. map ( |line| {
247
249
let comment = line. find ( "//" ) ;
248
250
if let Some ( comment_begins) = comment { & line[ 0 ..comment_begins] } else { line }
249
251
} )
250
- . any ( |code| code. contains ( "fn main" ) )
251
- } else {
252
- already_has_main
252
+ . any ( |code| code. contains ( "fn main" ) ) ;
253
253
} ;
254
254
255
255
Ok ( ( already_has_main, already_has_extern_crate) )
0 commit comments