Skip to content

Commit

Permalink
Ignore inline mod foo; modules
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Mar 31, 2023
1 parent 752b27f commit f9a006c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
33 changes: 19 additions & 14 deletions clippy_lints/src/items_after_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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_span: Option<Span> = None;
let mut when_was_visited = 0;

let hir = cx.tcx.hir();
Expand All @@ -53,29 +54,32 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
let item = hir.item(*itid);

if_chain! {
if was_test_mod_visited;
if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Use(_, _));
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 {
was_test_mod_visited = false;
span_lint_and_then(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", |diag| {
diag.multipart_suggestion("move the item to before the testing module was defined", vec![
(item.span, String::new()), // Remove the item
(
Span::new(
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_span.unwrap().lo()).file.name_hash;
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 {
was_test_mod_visited = false;
span_lint_and_then(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", |diag| {
diag.multipart_suggestion("move the item to before the testing module was defined", vec![
(item.span, String::new()), // Remove the item
(
Span::new(
hir.item(items[when_was_visited - 1]).span.hi() + BytePos(1),
hir.item(items[when_was_visited]).span.lo() - BytePos(1),
item.span.ctxt(), item.span.parent()),

format!("\n{}\n", snippet(cx, item.span, ".."))
) // ^ Copy the item to the new location
format!("\n{}\n", snippet(cx, item.span, ".."))
) // ^ Copy the item to the new location
], Applicability::MachineApplicable);
});
}
}

if matches!(item.kind, ItemKind::Mod(_)) {
if let ItemKind::Mod(mod_item) = item.kind {
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
if_chain! {
if attr.has_name(sym::cfg);
Expand All @@ -84,6 +88,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
if mitem.has_name(sym::test);
then {
was_test_mod_visited = true;
test_span = Some(mod_item.spans.inject_use_span);
when_was_visited = i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![allow(unused)]
#![warn(clippy::items_after_test_module)]

mod my_mod;

fn main() {}

fn should_not_lint() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![allow(unused)]
#![warn(clippy::items_after_test_module)]

mod my_mod;

fn main() {}

fn should_not_lint() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: an item was found after the testing module
--> $DIR/items_after_test_module.rs:16:1
--> $DIR/items_after_test_module.rs:18:1
|
LL | fn should_lint() {}
| ^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/items_after_test_modules/my_mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {}

#[cfg(test)]
mod my_test {
fn my_test() {}
}

0 comments on commit f9a006c

Please sign in to comment.