From 0c6da4ca873e7e90ab45d407fb1dd5c0b6da3aa3 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Wed, 12 Apr 2023 20:17:26 +0200 Subject: [PATCH] Compact emmited lint --- clippy_lints/src/items_after_test_module.rs | 33 +++++++++++---------- tests/ui/items_after_test_module.rs | 1 - tests/ui/items_after_test_module.stderr | 26 ++++++++-------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/clippy_lints/src/items_after_test_module.rs b/clippy_lints/src/items_after_test_module.rs index 9d949a44c020..52d716feea02 100644 --- a/clippy_lints/src/items_after_test_module.rs +++ b/clippy_lints/src/items_after_test_module.rs @@ -3,7 +3,7 @@ use rustc_hir::{HirId, ItemId, ItemKind, Mod}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::sym; +use rustc_span::{sym, Span}; declare_clippy_lint! { /// ### What it does @@ -43,38 +43,39 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]); impl LateLintPass<'_> for ItemsAfterTestModule { fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) { let mut was_test_mod_visited = false; - let mut test_mod_hash: Option = None; + let mut test_mod_span: Option = None; let hir = cx.tcx.hir(); let items = hir.items().collect::>(); - for itid in &items { + for (i, itid) in items.iter().enumerate() { let item = hir.item(*itid); if_chain! { if was_test_mod_visited; + if i == (items.len() - 3 /* Weird magic number (HIR-translation behaviour) */); if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash - == test_mod_hash.unwrap(); // Will never fail - if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Macro(_, _)); + == cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash; // Will never fail + if !matches!(item.kind, ItemKind::Mod(_)); if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself - if !in_external_macro(cx.sess(), item.span); + then { - span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", None, "move the item to before the testing module was defined"); + 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 Some(mitems) = attr.meta_item_list(); - if let [mitem] = &*mitems; - if mitem.has_name(sym::test); - then { - was_test_mod_visited = true; - test_mod_hash = Some(cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash); - } - } + 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); + } + } } } } diff --git a/tests/ui/items_after_test_module.rs b/tests/ui/items_after_test_module.rs index 735c06e51ffc..3d4e63e1828e 100644 --- a/tests/ui/items_after_test_module.rs +++ b/tests/ui/items_after_test_module.rs @@ -18,7 +18,6 @@ mod tests { fn should_lint() {} const SHOULD_ALSO_LINT: usize = 1; - macro_rules! should_not_lint { () => {}; } diff --git a/tests/ui/items_after_test_module.stderr b/tests/ui/items_after_test_module.stderr index 53ca4746476f..8f1616dabc1f 100644 --- a/tests/ui/items_after_test_module.stderr +++ b/tests/ui/items_after_test_module.stderr @@ -1,19 +1,17 @@ -error: an item was found after the testing module - --> $DIR/items_after_test_module.rs:18:1 +error: items were found after the testing module + --> $DIR/items_after_test_module.rs:13:1 | -LL | fn should_lint() {} - | ^^^^^^^^^^^^^^^^^^^ +LL | / mod tests { +LL | | #[test] +LL | | fn hi() {} +LL | | } +... | +LL | | () => {}; +LL | | } + | |_^ | - = help: move the item to before the testing module was defined + = help: move the items to before the testing module was defined = note: `-D clippy::items-after-test-module` implied by `-D warnings` -error: an item was found after the testing module - --> $DIR/items_after_test_module.rs:20:1 - | -LL | const SHOULD_ALSO_LINT: usize = 1; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move the item to before the testing module was defined - -error: aborting due to 2 previous errors +error: aborting due to previous error