mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Auto merge of #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`.
This commit is contained in:
commit
5459429f92
9 changed files with 97 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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)]`
|
||||
|
||||
|
|
|
@ -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)]`
|
||||
|
|
|
@ -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,
|
||||
|
@ -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())));
|
||||
|
|
|
@ -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
|
||||
|
@ -77,7 +83,7 @@ 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<'_>) {
|
||||
|
@ -85,7 +91,9 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
1
tests/ui-toml/panic/clippy.toml
Normal file
1
tests/ui-toml/panic/clippy.toml
Normal file
|
@ -0,0 +1 @@
|
|||
allow-panic-in-tests = true
|
54
tests/ui-toml/panic/panic.rs
Normal file
54
tests/ui-toml/panic/panic.rs
Normal file
|
@ -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
tests/ui-toml/panic/panic.stderr
Normal file
11
tests/ui-toml/panic/panic.stderr
Normal file
|
@ -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
|
||||
|
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue