@@ -44,6 +44,7 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);
44
44
impl LateLintPass < ' _ > for ItemsAfterTestModule {
45
45
fn check_mod ( & mut self , cx : & LateContext < ' _ > , _: & Mod < ' _ > , _: HirId ) {
46
46
let mut was_test_mod_visited = false ;
47
+ let mut test_span: Option < Span > = None ;
47
48
let mut when_was_visited = 0 ;
48
49
49
50
let hir = cx. tcx . hir ( ) ;
@@ -53,29 +54,32 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
53
54
let item = hir. item ( * itid) ;
54
55
55
56
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(
67
71
hir. item( items[ when_was_visited - 1 ] ) . span. hi( ) + BytePos ( 1 ) ,
68
72
hir. item( items[ when_was_visited] ) . span. lo( ) - BytePos ( 1 ) ,
69
73
item. span. ctxt( ) , item. span. parent( ) ) ,
70
74
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
73
77
] , Applicability :: MachineApplicable ) ;
74
78
} ) ;
75
79
}
76
80
}
77
81
78
- if matches ! ( item . kind , ItemKind :: Mod ( _ ) ) {
82
+ if let ItemKind :: Mod ( mod_item ) = item . kind {
79
83
for attr in cx. tcx . get_attrs ( item. owner_id . to_def_id ( ) , sym:: cfg) {
80
84
if_chain ! {
81
85
if attr. has_name( sym:: cfg) ;
@@ -84,6 +88,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
84
88
if mitem. has_name( sym:: test) ;
85
89
then {
86
90
was_test_mod_visited = true ;
91
+ test_span = Some ( mod_item. spans. inject_use_span) ;
87
92
when_was_visited = i;
88
93
}
89
94
}
0 commit comments