From 273c898aefeaec89b547f07bce98ad34aee8d38a Mon Sep 17 00:00:00 2001 From: blyxyas Date: Thu, 27 Apr 2023 16:43:51 +0200 Subject: [PATCH 1/4] Fix #10713 and move the tests to a subdir --- clippy_lints/src/items_after_test_module.rs | 15 ++++++++------- .../block_module.rs} | 0 .../block_module.stderr} | 2 +- .../items_after_test_module/imported_module.rs | 17 +++++++++++++++++ .../imported_module.stderr | 0 tests/ui/items_after_test_module/tests/mod.rs | 1 + 6 files changed, 27 insertions(+), 8 deletions(-) rename tests/ui/{items_after_test_module.rs => items_after_test_module/block_module.rs} (100%) rename tests/ui/{items_after_test_module.stderr => items_after_test_module/block_module.stderr} (89%) create mode 100644 tests/ui/items_after_test_module/imported_module.rs create mode 100644 tests/ui/items_after_test_module/imported_module.stderr create mode 100644 tests/ui/items_after_test_module/tests/mod.rs diff --git a/clippy_lints/src/items_after_test_module.rs b/clippy_lints/src/items_after_test_module.rs index 52d716feea02..96097575c9cb 100644 --- a/clippy_lints/src/items_after_test_module.rs +++ b/clippy_lints/src/items_after_test_module.rs @@ -64,20 +64,21 @@ impl LateLintPass<'_> for ItemsAfterTestModule { span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, test_mod_span.unwrap().with_hi(item.span.hi()), "items were found after the testing module", None, "move the items to before the testing module was defined"); }}; - if matches!(item.kind, ItemKind::Mod(_)) { - for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) { - if_chain! { - if attr.has_name(sym::cfg); + if let ItemKind::Mod(module) = item.kind && item.span.hi() == module.spans.inner_span.hi() { + // Check that it works the same way, the only I way I've found for #10713 + for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) { + if_chain! { + if attr.has_name(sym::cfg); if let Some(mitems) = attr.meta_item_list(); if let [mitem] = &*mitems; if mitem.has_name(sym::test); then { - was_test_mod_visited = true; - test_mod_span = Some(item.span); + was_test_mod_visited = true; + test_mod_span = Some(module.spans.inner_span.with_lo(item.span.lo())); } } } - } + } } } } diff --git a/tests/ui/items_after_test_module.rs b/tests/ui/items_after_test_module/block_module.rs similarity index 100% rename from tests/ui/items_after_test_module.rs rename to tests/ui/items_after_test_module/block_module.rs diff --git a/tests/ui/items_after_test_module.stderr b/tests/ui/items_after_test_module/block_module.stderr similarity index 89% rename from tests/ui/items_after_test_module.stderr rename to tests/ui/items_after_test_module/block_module.stderr index 8f1616dabc1f..597f1b9510c5 100644 --- a/tests/ui/items_after_test_module.stderr +++ b/tests/ui/items_after_test_module/block_module.stderr @@ -1,5 +1,5 @@ error: items were found after the testing module - --> $DIR/items_after_test_module.rs:13:1 + --> $DIR/block_module.rs:13:1 | LL | / mod tests { LL | | #[test] diff --git a/tests/ui/items_after_test_module/imported_module.rs b/tests/ui/items_after_test_module/imported_module.rs new file mode 100644 index 000000000000..819b6c8a9e08 --- /dev/null +++ b/tests/ui/items_after_test_module/imported_module.rs @@ -0,0 +1,17 @@ +//@compile-flags: --test +#![allow(unused)] +#![warn(clippy::items_after_test_module)] + +fn main() {} + +fn should_not_lint() {} + +#[cfg(test)] +mod tests; // Should not lint + +fn should_lint() {} + +const SHOULD_ALSO_LINT: usize = 1; +macro_rules! should_not_lint { + () => {}; +} diff --git a/tests/ui/items_after_test_module/imported_module.stderr b/tests/ui/items_after_test_module/imported_module.stderr new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/ui/items_after_test_module/tests/mod.rs b/tests/ui/items_after_test_module/tests/mod.rs new file mode 100644 index 000000000000..f328e4d9d04c --- /dev/null +++ b/tests/ui/items_after_test_module/tests/mod.rs @@ -0,0 +1 @@ +fn main() {} From f37054b396cc67f2633d929c35cd88ecdb383ba8 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Fri, 28 Apr 2023 20:19:36 +0200 Subject: [PATCH 2/4] Remove useless span magic --- clippy_lints/src/items_after_test_module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/items_after_test_module.rs b/clippy_lints/src/items_after_test_module.rs index 96097575c9cb..b992d689aa97 100644 --- a/clippy_lints/src/items_after_test_module.rs +++ b/clippy_lints/src/items_after_test_module.rs @@ -74,7 +74,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule { if mitem.has_name(sym::test); then { was_test_mod_visited = true; - test_mod_span = Some(module.spans.inner_span.with_lo(item.span.lo())); + test_mod_span = Some(item.span); } } } From 2b5820d58b62845ac23bda57533b44178c93cb7c Mon Sep 17 00:00:00 2001 From: blyxyas Date: Fri, 28 Apr 2023 20:23:05 +0200 Subject: [PATCH 3/4] Change module import system --- .../items_after_test_module/{tests/mod.rs => auxiliary/tests.rs} | 0 tests/ui/items_after_test_module/imported_module.rs | 1 + 2 files changed, 1 insertion(+) rename tests/ui/items_after_test_module/{tests/mod.rs => auxiliary/tests.rs} (100%) diff --git a/tests/ui/items_after_test_module/tests/mod.rs b/tests/ui/items_after_test_module/auxiliary/tests.rs similarity index 100% rename from tests/ui/items_after_test_module/tests/mod.rs rename to tests/ui/items_after_test_module/auxiliary/tests.rs diff --git a/tests/ui/items_after_test_module/imported_module.rs b/tests/ui/items_after_test_module/imported_module.rs index 819b6c8a9e08..22b1d3e2a9b6 100644 --- a/tests/ui/items_after_test_module/imported_module.rs +++ b/tests/ui/items_after_test_module/imported_module.rs @@ -6,6 +6,7 @@ fn main() {} fn should_not_lint() {} +#[path = "auxiliary/tests.rs"] #[cfg(test)] mod tests; // Should not lint From 395b1f5bf333e2ff168a3cf34592a816cab6016c Mon Sep 17 00:00:00 2001 From: blyxyas Date: Fri, 28 Apr 2023 20:40:47 +0200 Subject: [PATCH 4/4] Rename items + Delete `imported_module.stderr` --- tests/ui/items_after_test_module/imported_module.rs | 6 ++++-- tests/ui/items_after_test_module/imported_module.stderr | 0 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 tests/ui/items_after_test_module/imported_module.stderr diff --git a/tests/ui/items_after_test_module/imported_module.rs b/tests/ui/items_after_test_module/imported_module.rs index 22b1d3e2a9b6..6a757aef48e7 100644 --- a/tests/ui/items_after_test_module/imported_module.rs +++ b/tests/ui/items_after_test_module/imported_module.rs @@ -2,6 +2,8 @@ #![allow(unused)] #![warn(clippy::items_after_test_module)] +// Nothing here should lint, as `tests` is an imported module (that has no body). + fn main() {} fn should_not_lint() {} @@ -10,9 +12,9 @@ fn should_not_lint() {} #[cfg(test)] mod tests; // Should not lint -fn should_lint() {} +fn should_not_lint2() {} -const SHOULD_ALSO_LINT: usize = 1; +const SHOULD_ALSO_NOT_LINT: usize = 1; macro_rules! should_not_lint { () => {}; } diff --git a/tests/ui/items_after_test_module/imported_module.stderr b/tests/ui/items_after_test_module/imported_module.stderr deleted file mode 100644 index e69de29bb2d1..000000000000