Skip to content

Commit

Permalink
Ignore macros + minor performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Apr 14, 2023
1 parent 9e5d14e commit 53a0ccc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 13 additions & 13 deletions clippy_lints/src/items_after_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Span};
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -43,7 +43,7 @@ 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_span: Option<Span> = None;
let mut test_mod_hash: Option<u128> = None;

let hir = cx.tcx.hir();
let items = hir.items().collect::<Vec<ItemId>>();
Expand All @@ -54,8 +54,8 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
if_chain! {
if was_test_mod_visited;
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash
== cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash;
if !matches!(item.kind, ItemKind::Mod(_));
== test_mod_hash.unwrap(); // Will never fail
if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Macro(_, _));
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);
Expand All @@ -66,15 +66,15 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
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_span = Some(item.span);
}
}
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);
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/items_after_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ mod tests {
fn should_lint() {}

const SHOULD_ALSO_LINT: usize = 1;

macro_rules! should_not_lint {
() => {};
}

0 comments on commit 53a0ccc

Please sign in to comment.