From 1ac8dc51bc379136cbcba737e63ba619f9524269 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 | 3 +- tests/ui/items_after_test_module.stderr | 26 ++++++++-------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/clippy_lints/src/items_after_test_module.rs b/clippy_lints/src/items_after_test_module.rs index 9d949a44c02..52d716feea0 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 735c06e51ff..5136b2557ec 100644 --- a/tests/ui/items_after_test_module.rs +++ b/tests/ui/items_after_test_module.rs @@ -1,4 +1,4 @@ -// compile-flags: --test +//@compile-flags: --test #![allow(unused)] #![warn(clippy::items_after_test_module)] @@ -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 53ca4746476..8f1616dabc1 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