@@ -582,6 +582,7 @@ pub(crate) fn make_test(
582
582
dont_insert_main : bool ,
583
583
opts : & GlobalTestOptions ,
584
584
edition : Edition ,
585
+ // If `test_id` is `None`, it means we're generating code for a code example "run" link.
585
586
test_id : Option < & str > ,
586
587
) -> ( String , usize , bool ) {
587
588
let ( crate_attrs, everything_else, crates) = partition_source ( s, edition) ;
@@ -703,7 +704,7 @@ pub(crate) fn make_test(
703
704
( found_main, found_extern_crate, found_macro)
704
705
} )
705
706
} ) ;
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 {
707
708
// If the parser panicked due to a fatal error, pass the test code through unchanged.
708
709
// The error will be reported during compilation.
709
710
return ( s. to_owned ( ) , 0 , false ) ;
@@ -713,34 +714,34 @@ pub(crate) fn make_test(
713
714
// see it. In that case, run the old text-based scan to see if they at least have a main
714
715
// function written inside a macro invocation. See
715
716
// 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 ( )
718
720
. map ( |line| {
719
721
let comment = line. find ( "//" ) ;
720
722
if let Some ( comment_begins) = comment { & line[ 0 ..comment_begins] } else { line }
721
723
} )
722
- . any ( |code| code. contains ( "fn main" ) )
723
- } else {
724
- already_has_main
725
- } ;
724
+ . any ( |code| code. contains ( "fn main" ) ) ;
725
+ }
726
726
727
727
// Don't inject `extern crate std` because it's already injected by the
728
728
// 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 ;
744
745
}
745
746
746
747
// FIXME: This code cannot yet handle no_std test cases yet
0 commit comments