Skip to content

Commit 3594d55

Browse files
committed
Auto merge of #10719 - blyxyas:fix-items_after_test_mod_imported_modules, r=Alexendoo
Fix `items_after_test_module`: Ignore imported modules Fixes #10713. It does a little bit of dark magic, but intention is what really counts. changelog:[`items_after_test_module`]: Ignore imported modules (`mod foo;`) with no body.
2 parents b7939f4 + 395b1f5 commit 3594d55

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

clippy_lints/src/items_after_test_module.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,21 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
6464
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");
6565
}};
6666

67-
if matches!(item.kind, ItemKind::Mod(_)) {
68-
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
69-
if_chain! {
70-
if attr.has_name(sym::cfg);
67+
if let ItemKind::Mod(module) = item.kind && item.span.hi() == module.spans.inner_span.hi() {
68+
// Check that it works the same way, the only I way I've found for #10713
69+
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
70+
if_chain! {
71+
if attr.has_name(sym::cfg);
7172
if let Some(mitems) = attr.meta_item_list();
7273
if let [mitem] = &*mitems;
7374
if mitem.has_name(sym::test);
7475
then {
75-
was_test_mod_visited = true;
76+
was_test_mod_visited = true;
7677
test_mod_span = Some(item.span);
7778
}
7879
}
7980
}
80-
}
81+
}
8182
}
8283
}
8384
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

tests/ui/items_after_test_module.stderr renamed to tests/ui/items_after_test_module/block_module.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: items were found after the testing module
2-
--> $DIR/items_after_test_module.rs:13:1
2+
--> $DIR/block_module.rs:13:1
33
|
44
LL | / mod tests {
55
LL | | #[test]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@compile-flags: --test
2+
#![allow(unused)]
3+
#![warn(clippy::items_after_test_module)]
4+
5+
// Nothing here should lint, as `tests` is an imported module (that has no body).
6+
7+
fn main() {}
8+
9+
fn should_not_lint() {}
10+
11+
#[path = "auxiliary/tests.rs"]
12+
#[cfg(test)]
13+
mod tests; // Should not lint
14+
15+
fn should_not_lint2() {}
16+
17+
const SHOULD_ALSO_NOT_LINT: usize = 1;
18+
macro_rules! should_not_lint {
19+
() => {};
20+
}

0 commit comments

Comments
 (0)