Skip to content

Commit

Permalink
Compact emmited lint
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Apr 14, 2023
1 parent 53a0ccc commit 0c6da4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
33 changes: 17 additions & 16 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;
use rustc_span::{sym, Span};

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

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

for itid in &items {
for (i, itid) in items.iter().enumerate() {
let item = hir.item(*itid);

if_chain! {
if was_test_mod_visited;
if i == (items.len() - 3 /* Weird magic number (HIR-translation behaviour) */);
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash
== test_mod_hash.unwrap(); // Will never fail
if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Macro(_, _));
== cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash; // Will never fail
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 {
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");
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 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);
}
}
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);
}
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/items_after_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod tests {
fn should_lint() {}

const SHOULD_ALSO_LINT: usize = 1;

macro_rules! should_not_lint {
() => {};
}
26 changes: 12 additions & 14 deletions tests/ui/items_after_test_module.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
error: an item was found after the testing module
--> $DIR/items_after_test_module.rs:18:1
error: items were found after the testing module
--> $DIR/items_after_test_module.rs:13:1
|
LL | fn should_lint() {}
| ^^^^^^^^^^^^^^^^^^^
LL | / mod tests {
LL | | #[test]
LL | | fn hi() {}
LL | | }
... |
LL | | () => {};
LL | | }
| |_^
|
= help: move the item to before the testing module was defined
= help: move the items to before the testing module was defined
= note: `-D clippy::items-after-test-module` implied by `-D warnings`

error: an item was found after the testing module
--> $DIR/items_after_test_module.rs:20:1
|
LL | const SHOULD_ALSO_LINT: usize = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: move the item to before the testing module was defined

error: aborting due to 2 previous errors
error: aborting due to previous error

0 comments on commit 0c6da4c

Please sign in to comment.