Skip to content
/ rust Public
forked from rust-lang/rust

Commit 5459429

Browse files
committed
Auto merge of rust-lang#12803 - wutchzone:panic-in-tests, r=y21
Add configuration option for ignoring `panic!()` in tests ``` changelog: [`panic`]: Now can be disabled in tests with the `allow-panic-in-tests` option ``` I often find myself using `panic!(…)` in tests a lot, where I often do something like: ```rust match enam { Enam::A => …, Enam::B => …, _ => panic!("This should not happen at all."), } ``` I think this patch should go nicely with already existing `allow-unwrap-in-tests` and `allow-expect-in-tests`.
2 parents 6c8ff3f + c342a61 commit 5459429

File tree

9 files changed

+97
-4
lines changed

9 files changed

+97
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5942,6 +5942,7 @@ Released 2018-09-13
59425942
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests
59435943
[`allow-mixed-uninlined-format-args`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-mixed-uninlined-format-args
59445944
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
5945+
[`allow-panic-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-panic-in-tests
59455946
[`allow-print-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-print-in-tests
59465947
[`allow-private-module-inception`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-module-inception
59475948
[`allow-renamed-params-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-renamed-params-for

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ Whether to allow `r#""#` when `r""` can be used
101101
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
102102

103103

104+
## `allow-panic-in-tests`
105+
Whether `panic` should be allowed in test functions or `#[cfg(test)]`
106+
107+
**Default Value:** `false`
108+
109+
---
110+
**Affected lints:**
111+
* [`panic`](https://rust-lang.github.io/rust-clippy/master/index.html#panic)
112+
113+
104114
## `allow-print-in-tests`
105115
Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]`
106116

clippy_config/src/conf.rs

+4
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

+2-1
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

+11-3
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/panic/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-panic-in-tests = true

tests/ui-toml/panic/panic.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//@compile-flags: --test
2+
#![warn(clippy::panic)]
3+
4+
fn main() {
5+
enum Enam {
6+
A,
7+
}
8+
let a = Enam::A;
9+
match a {
10+
Enam::A => {},
11+
_ => panic!(""),
12+
}
13+
}
14+
15+
#[test]
16+
fn lonely_test() {
17+
enum Enam {
18+
A,
19+
}
20+
let a = Enam::A;
21+
match a {
22+
Enam::A => {},
23+
_ => panic!(""),
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
// should not lint in `#[cfg(test)]` modules
30+
#[test]
31+
fn test_fn() {
32+
enum Enam {
33+
A,
34+
}
35+
let a = Enam::A;
36+
match a {
37+
Enam::A => {},
38+
_ => panic!(""),
39+
}
40+
41+
bar();
42+
}
43+
44+
fn bar() {
45+
enum Enam {
46+
A,
47+
}
48+
let a = Enam::A;
49+
match a {
50+
Enam::A => {},
51+
_ => panic!(""),
52+
}
53+
}
54+
}

tests/ui-toml/panic/panic.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: `panic` should not be present in production code
2+
--> tests/ui-toml/panic/panic.rs:11:14
3+
|
4+
LL | _ => panic!(""),
5+
| ^^^^^^^^^^
6+
|
7+
= note: `-D clippy::panic` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::panic)]`
9+
10+
error: aborting due to 1 previous error
11+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
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)