Skip to content

Commit 59375ef

Browse files
committed
Add configuration option for ignoring panic!() in tests
1 parent c58b6e6 commit 59375ef

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

book/src/lint_configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ Whether `expect` should be allowed in test functions or `#[cfg(test)]`
8080
**Affected lints:**
8181
* [`expect_used`](https://rust-lang.github.io/rust-clippy/master/index.html#expect_used)
8282

83+
## `allow-panic-in-tests`
84+
Whether `panic` should be allowed in test functions or `#[cfg(test)]`
85+
86+
**Default Value:** `false`
87+
88+
---
89+
**Affected lints:**
90+
* [`panic`](https://rust-lang.github.io/rust-clippy/master/index.html#panic)
91+
8392

8493
## `allow-mixed-uninlined-format-args`
8594
Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)`

clippy_config/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ define_Conf! {
457457
///
458458
/// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]`
459459
(allow_unwrap_in_tests: bool = false),
460+
/// Lint: PANIC.
461+
///
462+
/// Whether `panic` should be allowed in test functions or `#[cfg(test)]`
463+
(allow_panic_in_tests: bool = false),
460464
/// Lint: DBG_MACRO.
461465
///
462466
/// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
532532
allow_expect_in_tests,
533533
allow_mixed_uninlined_format_args,
534534
allow_one_hash_in_raw_strings,
535+
allow_panic_in_tests,
535536
allow_print_in_tests,
536537
allow_private_module_inception,
537538
allow_unwrap_in_tests,
@@ -769,7 +770,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
769770
allow_in_test: allow_useless_vec_in_tests,
770771
})
771772
});
772-
store.register_late_pass(|_| Box::new(panic_unimplemented::PanicUnimplemented));
773+
store.register_late_pass(move |_| Box::new(panic_unimplemented::PanicUnimplemented { allow_panic_in_tests }));
773774
store.register_late_pass(|_| Box::new(strings::StringLitAsBytes));
774775
store.register_late_pass(|_| Box::new(derive::Derive));
775776
store.register_late_pass(move |_| Box::new(derivable_impls::DerivableImpls::new(msrv())));

clippy_lints/src/panic_unimplemented.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::is_in_test;
23
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
34
use rustc_hir::Expr;
45
use rustc_lint::{LateContext, LateLintPass};
5-
use rustc_session::declare_lint_pass;
6+
use rustc_session::impl_lint_pass;
7+
8+
#[derive(Clone)]
9+
pub struct PanicUnimplemented {
10+
pub allow_panic_in_tests: bool,
11+
}
612

713
declare_clippy_lint! {
814
/// ### What it does
@@ -77,15 +83,17 @@ declare_clippy_lint! {
7783
"usage of the `unreachable!` macro"
7884
}
7985

80-
declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
86+
impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
8187

8288
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
8389
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
8490
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
8591
return;
8692
};
8793
if is_panic(cx, macro_call.def_id) {
88-
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
94+
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
95+
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
96+
{
8997
return;
9098
}
9199

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
88
allow-expect-in-tests
99
allow-mixed-uninlined-format-args
1010
allow-one-hash-in-raw-strings
11+
allow-panic-in-tests
1112
allow-print-in-tests
1213
allow-private-module-inception
1314
allow-renamed-params-for
@@ -91,6 +92,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
9192
allow-expect-in-tests
9293
allow-mixed-uninlined-format-args
9394
allow-one-hash-in-raw-strings
95+
allow-panic-in-tests
9496
allow-print-in-tests
9597
allow-private-module-inception
9698
allow-renamed-params-for
@@ -174,6 +176,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
174176
allow-expect-in-tests
175177
allow-mixed-uninlined-format-args
176178
allow-one-hash-in-raw-strings
179+
allow-panic-in-tests
177180
allow-print-in-tests
178181
allow-private-module-inception
179182
allow-renamed-params-for

0 commit comments

Comments
 (0)