Compact emmited lint

This commit is contained in:
blyxyas 2023-04-12 20:17:26 +02:00
parent 0354cee137
commit 1ac8dc51bc
No known key found for this signature in database
GPG Key ID: 4D38170B5A2FC334
3 changed files with 30 additions and 32 deletions

View File

@ -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
@ -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);
}
}
}
}
}

View File

@ -1,4 +1,4 @@
// compile-flags: --test
//@compile-flags: --test
#![allow(unused)]
#![warn(clippy::items_after_test_module)]
@ -18,7 +18,6 @@ mod tests {
fn should_lint() {}
const SHOULD_ALSO_LINT: usize = 1;
macro_rules! should_not_lint {
() => {};
}

View File

@ -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