-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`.
- Loading branch information
Showing
9 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
allow-panic-in-tests = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(""), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters