|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_note, is_in_cfg_test, is_in_test_function, is_test_module_or_function}; |
| 2 | +use rustc_data_structures::sync::par_for_each_in; |
| 3 | +use rustc_hir::{intravisit::FnKind, Body, FnDecl, HirId, ItemKind, Mod}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | +use rustc_span::{def_id::LocalDefId, Span}; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// |
| 11 | + /// Triggers when a testing function (marked with the `#[test]` attribute) isn't inside a testing module (marked with `#[cfg(test)]`). |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// |
| 15 | + /// The idiomatic (and more performant) way of writing tests is inside a testing module (flagged with `#[cfg(test)]`), having test functions outside of this module is confusing and may lead to them being "hidden". |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// #[test] |
| 20 | + /// fn my_cool_test() { |
| 21 | + /// // [...] |
| 22 | + /// } |
| 23 | + /// |
| 24 | + /// #[cfg(test)] |
| 25 | + /// mod tests { |
| 26 | + /// // [...] |
| 27 | + /// } |
| 28 | + /// |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// #[cfg(test)] |
| 33 | + /// mod tests { |
| 34 | + /// #[test] |
| 35 | + /// fn my_cool_test() { |
| 36 | + /// // [...] |
| 37 | + /// } |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + #[clippy::version = "1.70.0"] |
| 41 | + pub TESTS_OUTSIDE_TEST_MODULE, |
| 42 | + restriction, |
| 43 | + "The test function `my_cool_test` is outside the testing module `tests`." |
| 44 | +} |
| 45 | + |
| 46 | +pub(crate) struct TestsOutsideTestModule { |
| 47 | + pub test_mod_exists: bool, |
| 48 | +} |
| 49 | + |
| 50 | +impl TestsOutsideTestModule { |
| 51 | + pub fn new() -> Self { |
| 52 | + Self { test_mod_exists: false } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl_lint_pass!(TestsOutsideTestModule => [TESTS_OUTSIDE_TEST_MODULE]); |
| 57 | + |
| 58 | +impl LateLintPass<'_> for TestsOutsideTestModule { |
| 59 | + fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) { |
| 60 | + self.test_mod_exists = false; |
| 61 | + |
| 62 | + // par_for_each_item uses Fn, while par_for_each_in uses FnMut |
| 63 | + par_for_each_in(cx.tcx.hir_crate_items(()).items(), |itemid| { |
| 64 | + let item = cx.tcx.hir().item(itemid); |
| 65 | + if_chain! { |
| 66 | + if matches!(item.kind, ItemKind::Mod(_)); |
| 67 | + if is_test_module_or_function(cx.tcx, item); |
| 68 | + then { |
| 69 | + self.test_mod_exists = true; |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + fn check_fn( |
| 76 | + &mut self, |
| 77 | + cx: &LateContext<'_>, |
| 78 | + kind: FnKind<'_>, |
| 79 | + _: &FnDecl<'_>, |
| 80 | + body: &Body<'_>, |
| 81 | + sp: Span, |
| 82 | + _: LocalDefId, |
| 83 | + ) { |
| 84 | + if_chain! { |
| 85 | + if !matches!(kind, FnKind::Closure); |
| 86 | + if self.test_mod_exists; |
| 87 | + if is_in_test_function(cx.tcx, body.id().hir_id); |
| 88 | + if !is_in_cfg_test(cx.tcx, body.id().hir_id); |
| 89 | + then { |
| 90 | + span_lint_and_note( |
| 91 | + cx, |
| 92 | + TESTS_OUTSIDE_TEST_MODULE, |
| 93 | + sp, |
| 94 | + "this function marked with #[test] is outside a #[cfg(test)] module", |
| 95 | + None, |
| 96 | + "move it to a testing module marked with #[cfg(test)]", |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments