Skip to content

Commit 4c0f747

Browse files
committed
Compact emmited lint
1 parent 02ba48d commit 4c0f747

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

clippy_lints/src/items_after_test_module.rs

+17-16
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;
6+
use rustc_span::{sym, Span};
77

88
declare_clippy_lint! {
99
/// ### What it does
@@ -43,38 +43,39 @@ 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_hash: Option<u128> = None;
46+
let mut test_mod_span: Option<Span> = None;
4747

4848
let hir = cx.tcx.hir();
4949
let items = hir.items().collect::<Vec<ItemId>>();
5050

51-
for itid in &items {
51+
for (i, itid) in items.iter().enumerate() {
5252
let item = hir.item(*itid);
5353

5454
if_chain! {
5555
if was_test_mod_visited;
56+
if i == (items.len() - 3 /* Weird magic number (HIR-translation behaviour) */);
5657
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash
57-
== test_mod_hash.unwrap(); // Will never fail
58-
if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Macro(_, _));
58+
== cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash; // Will never fail
59+
if !matches!(item.kind, ItemKind::Mod(_));
5960
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
60-
6161
if !in_external_macro(cx.sess(), item.span);
62+
6263
then {
63-
span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", None, "move the item to before the testing module was defined");
64+
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");
6465
}};
6566

6667
if matches!(item.kind, ItemKind::Mod(_)) {
6768
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
6869
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_hash = Some(cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash);
76-
}
77-
}
70+
if attr.has_name(sym::cfg);
71+
if let Some(mitems) = attr.meta_item_list();
72+
if let [mitem] = &*mitems;
73+
if mitem.has_name(sym::test);
74+
then {
75+
was_test_mod_visited = true;
76+
test_mod_span = Some(item.span);
77+
}
78+
}
7879
}
7980
}
8081
}

tests/ui/items_after_test_module.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod tests {
1818
fn should_lint() {}
1919

2020
const SHOULD_ALSO_LINT: usize = 1;
21-
2221
macro_rules! should_not_lint {
2322
() => {};
2423
}
+12-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
error: an item was found after the testing module
2-
--> $DIR/items_after_test_module.rs:18:1
1+
error: items were found after the testing module
2+
--> $DIR/items_after_test_module.rs:13:1
33
|
4-
LL | fn should_lint() {}
5-
| ^^^^^^^^^^^^^^^^^^^
4+
LL | / mod tests {
5+
LL | | #[test]
6+
LL | | fn hi() {}
7+
LL | | }
8+
... |
9+
LL | | () => {};
10+
LL | | }
11+
| |_^
612
|
7-
= help: move the item to before the testing module was defined
13+
= help: move the items to before the testing module was defined
814
= note: `-D clippy::items-after-test-module` implied by `-D warnings`
915

10-
error: an item was found after the testing module
11-
--> $DIR/items_after_test_module.rs:20:1
12-
|
13-
LL | const SHOULD_ALSO_LINT: usize = 1;
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: move the item to before the testing module was defined
17-
18-
error: aborting due to 2 previous errors
16+
error: aborting due to previous error
1917

0 commit comments

Comments
 (0)