Skip to content

Commit 38de6e6

Browse files
committed
Ignore inline mod foo; modules
1 parent 4d92edf commit 38de6e6

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

clippy_lints/src/items_after_test_module.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);
4444
impl 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
}

tests/ui/items_after_test_module.fixed renamed to tests/ui/items_after_test_modules/items_after_test_module.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![allow(unused)]
44
#![warn(clippy::items_after_test_module)]
55

6+
mod my_mod;
7+
68
fn main() {}
79

810
fn should_not_lint() {}

tests/ui/items_after_test_module.rs renamed to tests/ui/items_after_test_modules/items_after_test_module.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![allow(unused)]
44
#![warn(clippy::items_after_test_module)]
55

6+
mod my_mod;
7+
68
fn main() {}
79

810
fn should_not_lint() {}

tests/ui/items_after_test_module.stderr renamed to tests/ui/items_after_test_modules/items_after_test_module.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: an item was found after the testing module
2-
--> $DIR/items_after_test_module.rs:16:1
2+
--> $DIR/items_after_test_module.rs:18:1
33
|
44
LL | fn should_lint() {}
55
| ^^^^^^^^^^^^^^^^^^^
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {}
2+
3+
#[cfg(test)]
4+
mod my_test {
5+
fn my_test() {}
6+
}

0 commit comments

Comments
 (0)