|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 3 | +use clippy_utils::{is_in_cfg_test, is_in_test_function}; |
| 4 | +use rustc_hir::intravisit::FnKind; |
| 5 | +use rustc_hir::{Body, FnDecl}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::{ impl_lint_pass}; |
| 8 | +use rustc_span::Span; |
| 9 | +use rustc_span::def_id::LocalDefId; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for test functions (functions annotated with `#[test]`) that prefixed with `test_` |
| 14 | + /// which is redundant. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// This is redundant because the test functions are already annotated with `#[test]`. |
| 18 | + /// Moreover, it clutters the output of `cargo test` as test functions are expanded as |
| 19 | + /// `module::tests::test_name` in the output. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```no_run |
| 23 | + /// #[test] |
| 24 | + /// fn test_my_use_case() { |
| 25 | + /// // test code |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```no_run |
| 30 | + /// fn my_use_case() { |
| 31 | + /// // test code |
| 32 | + /// } |
| 33 | + /// ``` |
| 34 | + #[clippy::version = "1.84.0"] |
| 35 | + pub REDUNDANT_TEST_PREFIX, |
| 36 | + pedantic, |
| 37 | + "redundant `test_` prefix in test function name" |
| 38 | +} |
| 39 | + |
| 40 | +pub struct RedundantTestPrefix { |
| 41 | + in_integration_tests: bool, |
| 42 | +} |
| 43 | + |
| 44 | +impl RedundantTestPrefix { |
| 45 | + pub fn new(conf: &'static Conf) -> Self { |
| 46 | + Self { |
| 47 | + in_integration_tests: conf.redundant_test_prefix_in_integration_tests, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl_lint_pass!(RedundantTestPrefix => [REDUNDANT_TEST_PREFIX]); |
| 53 | + |
| 54 | +impl LateLintPass<'_> for RedundantTestPrefix { |
| 55 | + fn check_fn( |
| 56 | + &mut self, |
| 57 | + cx: &LateContext<'_>, |
| 58 | + fn_kind: FnKind<'_>, |
| 59 | + _: &FnDecl<'_>, |
| 60 | + body: &Body<'_>, |
| 61 | + span: Span, |
| 62 | + _: LocalDefId, |
| 63 | + ) { |
| 64 | + // Skip the lint if the function is not within a node with `#[cfg(test)]` attribute, |
| 65 | + // which is true for integration tests. If the lint is enabled for integration tests, |
| 66 | + // via configuration value, ignore this check. |
| 67 | + if !(self.in_integration_tests || is_in_cfg_test(cx.tcx, body.id().hir_id)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if is_in_test_function(cx.tcx, body.id().hir_id) && has_redundant_test_prefix(fn_kind) { |
| 72 | + span_lint_and_help( |
| 73 | + cx, |
| 74 | + REDUNDANT_TEST_PREFIX, |
| 75 | + span, |
| 76 | + "redundant `test_` prefix in test function name", |
| 77 | + None, |
| 78 | + "consider removing the `test_` prefix", |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Checks if the function has redundant `test_` prefix in its name. |
| 85 | +fn has_redundant_test_prefix(fn_kind: FnKind<'_>) -> bool { |
| 86 | + matches!(fn_kind, FnKind::ItemFn(ident, ..) if ident.name.as_str().starts_with("test_")) |
| 87 | +} |
0 commit comments