|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::macros::{is_panic, root_macro_call_first_node}; |
| 3 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 4 | +use clippy_utils::visitors::Visitable; |
| 5 | +use clippy_utils::{is_in_test_function, method_chain_args}; |
| 6 | +use rustc_hir::intravisit::{self, FnKind, Visitor}; |
| 7 | +use rustc_hir::{AnonConst, Body, Expr, FnDecl, Item, ItemKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::hir::nested_filter; |
| 10 | +use rustc_middle::ty; |
| 11 | +use rustc_session::declare_lint_pass; |
| 12 | +use rustc_span::{Span, sym}; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// Triggers when a testing function (marked with the `#[test]` attribute) does not have a way to fail. |
| 17 | + /// |
| 18 | + /// ### Why restrict this? |
| 19 | + /// If a test does not have a way to fail, the developer might be getting false positives from their test suites. |
| 20 | + /// The idiomatic way of using test functions should be such that they actually can fail in an erroneous state. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```no_run |
| 24 | + /// #[test] |
| 25 | + /// fn my_cool_test() { |
| 26 | + /// // [...] |
| 27 | + /// } |
| 28 | + /// |
| 29 | + /// #[cfg(test)] |
| 30 | + /// mod tests { |
| 31 | + /// // [...] |
| 32 | + /// } |
| 33 | + /// |
| 34 | + /// ``` |
| 35 | + /// Use instead: |
| 36 | + /// ```no_run |
| 37 | + /// #[cfg(test)] |
| 38 | + /// mod tests { |
| 39 | + /// #[test] |
| 40 | + /// fn my_cool_test() { |
| 41 | + /// // [...] |
| 42 | + /// } |
| 43 | + /// } |
| 44 | + /// ``` |
| 45 | + #[clippy::version = "1.70.0"] |
| 46 | + pub TEST_WITHOUT_FAIL_CASE, |
| 47 | + restriction, |
| 48 | + "A test function is outside the testing module." |
| 49 | +} |
| 50 | + |
| 51 | +declare_lint_pass!(TestWithoutFailCase => [TEST_WITHOUT_FAIL_CASE]); |
| 52 | + |
| 53 | +/* |
| 54 | +impl<'tcx> LateLintPass<'tcx> for TestWithoutFailCase { |
| 55 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx rustc_hir::Item<'_>) { |
| 56 | + if let rustc_hir::ItemKind::Fn(sig, _, body_id) = item.kind { |
| 57 | +
|
| 58 | + } |
| 59 | + if is_in_test_function(cx.tcx, item.hir_id()) { |
| 60 | + let typck = cx.tcx.typeck(item.id.owner_id.def_id); |
| 61 | + let find_panic_visitor = FindPanicUnwrap::find_span(cx, typck, item.body) |
| 62 | + span_lint(cx, TEST_WITHOUT_FAIL_CASE, item.span, "test function cannot panic"); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + */ |
| 67 | + |
| 68 | +impl<'tcx> LateLintPass<'tcx> for TestWithoutFailCase { |
| 69 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 70 | + if let ItemKind::Fn(_, _, body_id) = item.kind |
| 71 | + && is_in_test_function(cx.tcx, item.hir_id()) |
| 72 | + { |
| 73 | + let body = cx.tcx.hir().body(body_id); |
| 74 | + let typ = cx.tcx.typeck(item.owner_id); |
| 75 | + let panic_span = FindPanicUnwrap::find_span(cx, typ, body); |
| 76 | + if panic_span.is_none() { |
| 77 | + // No way to panic for this test. |
| 78 | + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] |
| 79 | + span_lint_and_then( |
| 80 | + cx, |
| 81 | + TEST_WITHOUT_FAIL_CASE, |
| 82 | + item.span, |
| 83 | + "this function marked with #[test] has no way to fail", |
| 84 | + |diag| { |
| 85 | + diag.note("make sure that something is checked in this test"); |
| 86 | + }, |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +struct FindPanicUnwrap<'a, 'tcx> { |
| 94 | + cx: &'a LateContext<'tcx>, |
| 95 | + is_const: bool, |
| 96 | + panic_span: Option<Span>, |
| 97 | + typeck_results: &'tcx ty::TypeckResults<'tcx>, |
| 98 | +} |
| 99 | + |
| 100 | +impl<'a, 'tcx> FindPanicUnwrap<'a, 'tcx> { |
| 101 | + pub fn find_span( |
| 102 | + cx: &'a LateContext<'tcx>, |
| 103 | + typeck_results: &'tcx ty::TypeckResults<'tcx>, |
| 104 | + body: impl Visitable<'tcx>, |
| 105 | + ) -> Option<(Span, bool)> { |
| 106 | + let mut vis = Self { |
| 107 | + cx, |
| 108 | + is_const: false, |
| 109 | + panic_span: None, |
| 110 | + typeck_results, |
| 111 | + }; |
| 112 | + body.visit(&mut vis); |
| 113 | + vis.panic_span.map(|el| (el, vis.is_const)) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> { |
| 118 | + type NestedFilter = nested_filter::OnlyBodies; |
| 119 | + |
| 120 | + fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { |
| 121 | + if self.panic_span.is_some() { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if let Some(macro_call) = root_macro_call_first_node(self.cx, expr) { |
| 126 | + if is_panic(self.cx, macro_call.def_id) |
| 127 | + || matches!( |
| 128 | + self.cx.tcx.item_name(macro_call.def_id).as_str(), |
| 129 | + "assert" | "assert_eq" | "assert_ne" |
| 130 | + ) |
| 131 | + { |
| 132 | + self.is_const = self.cx.tcx.hir().is_inside_const_context(expr.hir_id); |
| 133 | + self.panic_span = Some(macro_call.span); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // check for `unwrap` and `expect` for both `Option` and `Result` |
| 138 | + if let Some(arglists) = method_chain_args(expr, &["unwrap"]).or(method_chain_args(expr, &["expect"])) { |
| 139 | + let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs(); |
| 140 | + if is_type_diagnostic_item(self.cx, receiver_ty, sym::Option) |
| 141 | + || is_type_diagnostic_item(self.cx, receiver_ty, sym::Result) |
| 142 | + { |
| 143 | + self.panic_span = Some(expr.span); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // and check sub-expressions |
| 148 | + intravisit::walk_expr(self, expr); |
| 149 | + } |
| 150 | + |
| 151 | + // Panics in const blocks will cause compilation to fail. |
| 152 | + fn visit_anon_const(&mut self, _: &'tcx AnonConst) {} |
| 153 | + |
| 154 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 155 | + self.cx.tcx.hir() |
| 156 | + } |
| 157 | +} |
0 commit comments