Skip to content

Fix items_after_test_module: Ignore imported modules #10719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions clippy_lints/src/items_after_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,21 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
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 ItemKind::Mod(module) = item.kind && item.span.hi() == module.spans.inner_span.hi() {
// Check that it works the same way, the only I way I've found for #10713
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;
was_test_mod_visited = true;
test_mod_span = Some(item.span);
}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/ui/items_after_test_module/auxiliary/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: items were found after the testing module
--> $DIR/items_after_test_module.rs:13:1
--> $DIR/block_module.rs:13:1
|
LL | / mod tests {
LL | | #[test]
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/items_after_test_module/imported_module.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a test in ui-cargo rather than ui for this, that way the module isn't being itself compiled as a test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That or put the module in auxiliary and import it with #[path = "auxiliary/tests.rs"] mod tests;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@compile-flags: --test
#![allow(unused)]
#![warn(clippy::items_after_test_module)]

// Nothing here should lint, as `tests` is an imported module (that has no body).

fn main() {}

fn should_not_lint() {}

#[path = "auxiliary/tests.rs"]
#[cfg(test)]
mod tests; // Should not lint

fn should_not_lint2() {}

const SHOULD_ALSO_NOT_LINT: usize = 1;
macro_rules! should_not_lint {
() => {};
}