|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_then, is_in_cfg_test, source::snippet}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{HirId, ItemId, ItemKind, Mod}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 5 | +use rustc_middle::lint::in_external_macro; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::{sym, BytePos, Span}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Triggers if an item is declared after the testing module marked with `#[cfg(test)]`. |
| 12 | + /// ### Why is this bad? |
| 13 | + /// Having items declared after the testing module is confusing and may lead to bad test coverage. |
| 14 | + /// ### Example |
| 15 | + /// ```rust |
| 16 | + /// #[cfg(test)] |
| 17 | + /// mod tests { |
| 18 | + /// // [...] |
| 19 | + /// } |
| 20 | + /// |
| 21 | + /// fn my_function() { |
| 22 | + /// // [...] |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// fn my_function() { |
| 28 | + /// // [...] |
| 29 | + /// } |
| 30 | + /// |
| 31 | + /// #[cfg(test)] |
| 32 | + /// mod tests { |
| 33 | + /// // [...] |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + #[clippy::version = "1.70.0"] |
| 37 | + pub ITEMS_AFTER_TEST_MODULE, |
| 38 | + style, |
| 39 | + "An item was found after the testing module `tests`" |
| 40 | +} |
| 41 | + |
| 42 | +declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]); |
| 43 | + |
| 44 | +impl LateLintPass<'_> for ItemsAfterTestModule { |
| 45 | + fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) { |
| 46 | + let mut was_test_mod_visited = false; |
| 47 | + let mut when_was_visited = 0; |
| 48 | + |
| 49 | + let hir = cx.tcx.hir(); |
| 50 | + let items = hir.items().collect::<Vec<ItemId>>(); |
| 51 | + |
| 52 | + for (i, itid) in items.iter().enumerate() { |
| 53 | + let item = hir.item(*itid); |
| 54 | + |
| 55 | + if_chain! { |
| 56 | + if was_test_mod_visited; |
| 57 | + if !matches!(item.kind, ItemKind::Mod(_) | ItemKind::Use(_, _)); |
| 58 | + if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself |
| 59 | + if !in_external_macro(cx.sess(), item.span); |
| 60 | + then { |
| 61 | + was_test_mod_visited = false; |
| 62 | + span_lint_and_then(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", |diag| { |
| 63 | + diag.multipart_suggestion("move the item to before the testing module was defined", vec![ |
| 64 | + (item.span, String::new()), // Remove the item |
| 65 | + ( |
| 66 | + Span::new( |
| 67 | + hir.item(items[when_was_visited - 1]).span.hi() + BytePos(1), |
| 68 | + hir.item(items[when_was_visited]).span.lo() - BytePos(1), |
| 69 | + item.span.ctxt(), item.span.parent()), |
| 70 | + |
| 71 | + format!("\n{}\n", snippet(cx, item.span, "..")) |
| 72 | + ) // ^ Copy the item to the new location |
| 73 | + ], Applicability::MachineApplicable); |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if matches!(item.kind, ItemKind::Mod(_)) { |
| 79 | + for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) { |
| 80 | + if_chain! { |
| 81 | + if attr.has_name(sym::cfg); |
| 82 | + if let Some(mitems) = attr.meta_item_list(); |
| 83 | + if let [mitem] = &*mitems; |
| 84 | + if mitem.has_name(sym::test); |
| 85 | + then { |
| 86 | + was_test_mod_visited = true; |
| 87 | + when_was_visited = i; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments