Skip to content

Commit

Permalink
Auto merge of #12803 - wutchzone:panic-in-tests, r=y21
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bors committed May 16, 2024
2 parents caad063 + c342a61 commit 430c885
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5942,6 +5942,7 @@ Released 2018-09-13
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests
[`allow-mixed-uninlined-format-args`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-mixed-uninlined-format-args
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
[`allow-panic-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-panic-in-tests
[`allow-print-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-print-in-tests
[`allow-private-module-inception`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-module-inception
[`allow-renamed-params-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-renamed-params-for
Expand Down
10 changes: 10 additions & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ Whether to allow `r#""#` when `r""` can be used
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)


## `allow-panic-in-tests`
Whether `panic` should be allowed in test functions or `#[cfg(test)]`

**Default Value:** `false`

---
**Affected lints:**
* [`panic`](https://rust-lang.github.io/rust-clippy/master/index.html#panic)


## `allow-print-in-tests`
Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]`

Expand Down
4 changes: 4 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ define_Conf! {
///
/// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]`
(allow_unwrap_in_tests: bool = false),
/// Lint: PANIC.
///
/// Whether `panic` should be allowed in test functions or `#[cfg(test)]`
(allow_panic_in_tests: bool = false),
/// Lint: DBG_MACRO.
///
/// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
allow_expect_in_tests,
allow_mixed_uninlined_format_args,
allow_one_hash_in_raw_strings,
allow_panic_in_tests,
allow_print_in_tests,
allow_private_module_inception,
allow_unwrap_in_tests,
Expand Down Expand Up @@ -769,7 +770,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
allow_in_test: allow_useless_vec_in_tests,
})
});
store.register_late_pass(|_| Box::new(panic_unimplemented::PanicUnimplemented));
store.register_late_pass(move |_| Box::new(panic_unimplemented::PanicUnimplemented { allow_panic_in_tests }));
store.register_late_pass(|_| Box::new(strings::StringLitAsBytes));
store.register_late_pass(|_| Box::new(derive::Derive));
store.register_late_pass(move |_| Box::new(derivable_impls::DerivableImpls::new(msrv())));
Expand Down
14 changes: 11 additions & 3 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test;
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_session::impl_lint_pass;

#[derive(Clone)]
pub struct PanicUnimplemented {
pub allow_panic_in_tests: bool,
}

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -77,15 +83,17 @@ declare_clippy_lint! {
"usage of the `unreachable!` macro"
}

declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);

impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
return;
};
if is_panic(cx, macro_call.def_id) {
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
{
return;
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/panic/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow-panic-in-tests = true
54 changes: 54 additions & 0 deletions tests/ui-toml/panic/panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//@compile-flags: --test
#![warn(clippy::panic)]

fn main() {
enum Enam {
A,
}
let a = Enam::A;
match a {
Enam::A => {},
_ => panic!(""),
}
}

#[test]
fn lonely_test() {
enum Enam {
A,
}
let a = Enam::A;
match a {
Enam::A => {},
_ => panic!(""),
}
}

#[cfg(test)]
mod tests {
// should not lint in `#[cfg(test)]` modules
#[test]
fn test_fn() {
enum Enam {
A,
}
let a = Enam::A;
match a {
Enam::A => {},
_ => panic!(""),
}

bar();
}

fn bar() {
enum Enam {
A,
}
let a = Enam::A;
match a {
Enam::A => {},
_ => panic!(""),
}
}
}
11 changes: 11 additions & 0 deletions tests/ui-toml/panic/panic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `panic` should not be present in production code
--> tests/ui-toml/panic/panic.rs:11:14
|
LL | _ => panic!(""),
| ^^^^^^^^^^
|
= note: `-D clippy::panic` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::panic)]`

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
allow-expect-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
Expand Down Expand Up @@ -91,6 +92,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
allow-expect-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
Expand Down Expand Up @@ -174,6 +176,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
allow-expect-in-tests
allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
Expand Down

0 comments on commit 430c885

Please sign in to comment.