|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::is_test_function; |
| 3 | +use rustc_hir::intravisit::FnKind; |
| 4 | +use rustc_hir::{Body, ExprKind, FnDecl}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::declare_lint_pass; |
| 7 | +use rustc_span::Span; |
| 8 | +use rustc_span::def_id::LocalDefId; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for test functions with an empty body. |
| 13 | + /// |
| 14 | + /// ### Why restrict this? |
| 15 | + /// Empty tests do not verify behavior. However, they can serve as temporary placeholders while |
| 16 | + /// a test is being developed. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// #[test] |
| 21 | + /// fn empty_test() {} |
| 22 | + /// ``` |
| 23 | + /// |
| 24 | + /// Remove the test if it is no longer needed, or add assertions that exercise the behavior it |
| 25 | + /// is intended to test. |
| 26 | + #[clippy::version = "1.100.0"] |
| 27 | + pub EMPTY_TEST, |
| 28 | + restriction, |
| 29 | + "test function with an empty body" |
| 30 | +} |
| 31 | + |
| 32 | +declare_lint_pass!(EmptyTest => [EMPTY_TEST]); |
| 33 | + |
| 34 | +impl LateLintPass<'_> for EmptyTest { |
| 35 | + fn check_fn( |
| 36 | + &mut self, |
| 37 | + cx: &LateContext<'_>, |
| 38 | + kind: FnKind<'_>, |
| 39 | + _: &FnDecl<'_>, |
| 40 | + body: &Body<'_>, |
| 41 | + span: Span, |
| 42 | + fn_def_id: LocalDefId, |
| 43 | + ) { |
| 44 | + if matches!(kind, FnKind::ItemFn(..)) |
| 45 | + && is_test_function(cx.tcx, fn_def_id) |
| 46 | + && matches!(body.value.kind, ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_none()) |
| 47 | + { |
| 48 | + span_lint(cx, EMPTY_TEST, span, "empty test function"); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments