Add configuration option for ignoring panic!() in tests

This commit is contained in:
Daniel Sedlak 2024-05-15 10:59:35 +00:00
parent c58b6e66fd
commit c342a61564
9 changed files with 97 additions and 4 deletions

View File

@ -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-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-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-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-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-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 [`allow-renamed-params-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-renamed-params-for

View File

@ -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) * [`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` ## `allow-print-in-tests`
Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]` Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]`

View File

@ -457,6 +457,10 @@ pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
/// ///
/// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]` /// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]`
(allow_unwrap_in_tests: bool = false), (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. /// Lint: DBG_MACRO.
/// ///
/// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]` /// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`

View File

@ -532,6 +532,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
allow_expect_in_tests, allow_expect_in_tests,
allow_mixed_uninlined_format_args, allow_mixed_uninlined_format_args,
allow_one_hash_in_raw_strings, allow_one_hash_in_raw_strings,
allow_panic_in_tests,
allow_print_in_tests, allow_print_in_tests,
allow_private_module_inception, allow_private_module_inception,
allow_unwrap_in_tests, 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, 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(strings::StringLitAsBytes));
store.register_late_pass(|_| Box::new(derive::Derive)); store.register_late_pass(|_| Box::new(derive::Derive));
store.register_late_pass(move |_| Box::new(derivable_impls::DerivableImpls::new(msrv()))); store.register_late_pass(move |_| Box::new(derivable_impls::DerivableImpls::new(msrv())));

View File

@ -1,8 +1,14 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test;
use clippy_utils::macros::{is_panic, root_macro_call_first_node}; use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use rustc_hir::Expr; use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass}; 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! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -77,7 +83,7 @@
"usage of the `unreachable!` macro" "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 { impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@ -85,7 +91,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
return; return;
}; };
if is_panic(cx, macro_call.def_id) { 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; return;
} }

View File

@ -0,0 +1 @@
allow-panic-in-tests = true

View 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!(""),
}
}
}

View 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

View File

@ -8,6 +8,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
allow-expect-in-tests allow-expect-in-tests
allow-mixed-uninlined-format-args allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests allow-print-in-tests
allow-private-module-inception allow-private-module-inception
allow-renamed-params-for allow-renamed-params-for
@ -91,6 +92,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
allow-expect-in-tests allow-expect-in-tests
allow-mixed-uninlined-format-args allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests allow-print-in-tests
allow-private-module-inception allow-private-module-inception
allow-renamed-params-for 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-expect-in-tests
allow-mixed-uninlined-format-args allow-mixed-uninlined-format-args
allow-one-hash-in-raw-strings allow-one-hash-in-raw-strings
allow-panic-in-tests
allow-print-in-tests allow-print-in-tests
allow-private-module-inception allow-private-module-inception
allow-renamed-params-for allow-renamed-params-for