Skip to content

Commit 53a0ccc

Browse files
committed
Ignore macros + minor performance improvement
1 parent 9e5d14e commit 53a0ccc

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

clippy_lints/src/items_after_test_module.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::{HirId, ItemId, ItemKind, Mod};
33
use rustc_lint::{LateContext, LateLintPass, LintContext};
44
use rustc_middle::lint::in_external_macro;
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
6-
use rustc_span::{sym, Span};
6+
use rustc_span::sym;
77

88
declare_clippy_lint! {
99
/// ### What it does
@@ -43,7 +43,7 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);
4343
impl LateLintPass<'_> for ItemsAfterTestModule {
4444
fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) {
4545
let mut was_test_mod_visited = false;
46-
let mut test_mod_span: Option<Span> = None;
46+
let mut test_mod_hash: Option<u128> = None;
4747

4848
let hir = cx.tcx.hir();
4949
let items = hir.items().collect::<Vec<ItemId>>();
@@ -54,8 +54,8 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
5454
if_chain! {
5555
if was_test_mod_visited;
5656
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash
57-
== cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash;
58-
if !matches!(item.kind, ItemKind::Mod(_));
57+
== test_mod_hash.unwrap(); // Will never fail
58+
if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Macro(_, _));
5959
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
6060

6161
if !in_external_macro(cx.sess(), item.span);
@@ -66,15 +66,15 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
6666
if matches!(item.kind, ItemKind::Mod(_)) {
6767
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
6868
if_chain! {
69-
if attr.has_name(sym::cfg);
70-
if let Some(mitems) = attr.meta_item_list();
71-
if let [mitem] = &*mitems;
72-
if mitem.has_name(sym::test);
73-
then {
74-
was_test_mod_visited = true;
75-
test_mod_span = Some(item.span);
76-
}
77-
}
69+
if attr.has_name(sym::cfg);
70+
if let Some(mitems) = attr.meta_item_list();
71+
if let [mitem] = &*mitems;
72+
if mitem.has_name(sym::test);
73+
then {
74+
was_test_mod_visited = true;
75+
test_mod_hash = Some(cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash);
76+
}
77+
}
7878
}
7979
}
8080
}

tests/ui/items_after_test_module.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ mod tests {
1818
fn should_lint() {}
1919

2020
const SHOULD_ALSO_LINT: usize = 1;
21+
22+
macro_rules! should_not_lint {
23+
() => {};
24+
}

0 commit comments

Comments
 (0)