@@ -44,6 +44,7 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);
4444impl LateLintPass < ' _ > for ItemsAfterTestModule {
4545 fn check_mod ( & mut self , cx : & LateContext < ' _ > , _: & Mod < ' _ > , _: HirId ) {
4646 let mut was_test_mod_visited = false ;
47+ let mut test_span: Option < Span > = None ;
4748 let mut when_was_visited = 0 ;
4849
4950 let hir = cx. tcx . hir ( ) ;
@@ -53,29 +54,32 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
5354 let item = hir. item ( * itid) ;
5455
5556 if_chain ! {
56- if was_test_mod_visited;
57- if !matches!( item. kind, ItemKind :: Mod ( _) | ItemKind :: Use ( _, _) ) ;
58- if !is_in_cfg_test( cx. tcx, itid. hir_id( ) ) ; // The item isn't in the testing module itself
59- if !in_external_macro( cx. sess( ) , item. span) ;
60- then {
61- was_test_mod_visited = false ;
62- span_lint_and_then( cx, ITEMS_AFTER_TEST_MODULE , item. span, "an item was found after the testing module" , |diag| {
63- diag. multipart_suggestion( "move the item to before the testing module was defined" , vec![
64- ( item. span, String :: new( ) ) , // Remove the item
65- (
66- Span :: new(
57+ if was_test_mod_visited;
58+ if cx. sess( ) . source_map( ) . lookup_char_pos( item. span. lo( ) ) . file. name_hash ==
59+ cx. sess( ) . source_map( ) . lookup_char_pos( test_span. unwrap( ) . lo( ) ) . file. name_hash;
60+ if !matches!( item. kind, ItemKind :: Mod ( _) ) ;
61+ if !is_in_cfg_test( cx. tcx, itid. hir_id( ) ) ; // The item isn't in the testing module itself
62+
63+ if !in_external_macro( cx. sess( ) , item. span) ;
64+ then {
65+ was_test_mod_visited = false ;
66+ span_lint_and_then( cx, ITEMS_AFTER_TEST_MODULE , item. span, "an item was found after the testing module" , |diag| {
67+ diag. multipart_suggestion( "move the item to before the testing module was defined" , vec![
68+ ( item. span, String :: new( ) ) , // Remove the item
69+ (
70+ Span :: new(
6771 hir. item( items[ when_was_visited - 1 ] ) . span. hi( ) + BytePos ( 1 ) ,
6872 hir. item( items[ when_was_visited] ) . span. lo( ) - BytePos ( 1 ) ,
6973 item. span. ctxt( ) , item. span. parent( ) ) ,
7074
71- format!( "\n {}\n " , snippet( cx, item. span, ".." ) )
72- ) // ^ Copy the item to the new location
75+ format!( "\n {}\n " , snippet( cx, item. span, ".." ) )
76+ ) // ^ Copy the item to the new location
7377 ] , Applicability :: MachineApplicable ) ;
7478 } ) ;
7579 }
7680 }
7781
78- if matches ! ( item . kind , ItemKind :: Mod ( _ ) ) {
82+ if let ItemKind :: Mod ( mod_item ) = item . kind {
7983 for attr in cx. tcx . get_attrs ( item. owner_id . to_def_id ( ) , sym:: cfg) {
8084 if_chain ! {
8185 if attr. has_name( sym:: cfg) ;
@@ -84,6 +88,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
8488 if mitem. has_name( sym:: test) ;
8589 then {
8690 was_test_mod_visited = true ;
91+ test_span = Some ( mod_item. spans. inject_use_span) ;
8792 when_was_visited = i;
8893 }
8994 }
0 commit comments