Skip to content

Commit d00f467

Browse files
committed
Add configuration option to allow unreachable! in test functions
1 parent 9a2076e commit d00f467

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ define_Conf! {
403403
#[lints(renamed_function_params)]
404404
allow_renamed_params_for: Vec<String> =
405405
DEFAULT_ALLOWED_TRAITS_WITH_RENAMED_PARAMS.iter().map(ToString::to_string).collect(),
406+
/// Whether `unreachable` should be allowed in test functions or `#[cfg(test)]`
407+
#[lints(unreachable)]
408+
allow_unreachable_in_tests: bool = false,
406409
/// Whether `unwrap` should be allowed in code always evaluated at compile time
407410
#[lints(unwrap_used)]
408411
allow_unwrap_in_consts: bool = true,

clippy_lints/src/panic_unimplemented.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ use rustc_span::sym;
1010

1111
pub struct PanicUnimplemented {
1212
allow_panic_in_tests: bool,
13+
allow_unreachable_in_tests: bool,
1314
}
1415

1516
impl PanicUnimplemented {
1617
pub fn new(conf: &'static Conf) -> Self {
1718
Self {
1819
allow_panic_in_tests: conf.allow_panic_in_tests,
20+
allow_unreachable_in_tests: conf.allow_unreachable_in_tests,
1921
}
2022
}
2123
}
@@ -131,6 +133,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
131133
);
132134
},
133135
Some(sym::unreachable_macro) => {
136+
if self.allow_unreachable_in_tests && is_in_test(cx.tcx, expr.hir_id) {
137+
return;
138+
}
139+
134140
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
135141
},
136142
_ => {},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-unreachable-in-tests = true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@compile-flags: --test
2+
#![warn(clippy::unreachable)]
3+
4+
fn main() {
5+
unreachable!();
6+
//~^ unreachable
7+
}
8+
9+
#[test]
10+
fn allowed_in_test_fn() {
11+
unreachable!();
12+
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
#[test]
17+
fn nested_test() {
18+
unreachable!();
19+
}
20+
21+
fn helper() {
22+
// still test context should be allowed
23+
unreachable!();
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: usage of the `unreachable!` macro
2+
--> tests/ui-toml/unreachable/unreachable.rs:5:5
3+
|
4+
LL | unreachable!();
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::unreachable` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)