Skip to content

Commit 329a582

Browse files
committed
Add configuration option to allow unreachable! in test functions
1 parent 9bfa95b commit 329a582

File tree

8 files changed

+60
-0
lines changed

8 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6692,6 +6692,7 @@ Released 2018-09-13
66926692
[`allow-print-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-print-in-tests
66936693
[`allow-private-module-inception`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-module-inception
66946694
[`allow-renamed-params-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-renamed-params-for
6695+
[`allow-unreachable-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unreachable-in-tests
66956696
[`allow-unwrap-in-consts`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unwrap-in-consts
66966697
[`allow-unwrap-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unwrap-in-tests
66976698
[`allow-useless-vec-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-useless-vec-in-tests

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ default configuration of Clippy. By default, any configuration will replace the
184184
* [`renamed_function_params`](https://rust-lang.github.io/rust-clippy/master/index.html#renamed_function_params)
185185

186186

187+
## `allow-unreachable-in-tests`
188+
Whether `unreachable` should be allowed in test functions or `#[cfg(test)]`
189+
190+
**Default Value:** `false`
191+
192+
---
193+
**Affected lints:**
194+
* [`unreachable`](https://rust-lang.github.io/rust-clippy/master/index.html#unreachable)
195+
196+
187197
## `allow-unwrap-in-consts`
188198
Whether `unwrap` should be allowed in code always evaluated at compile time
189199

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
_ => {},

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
1515
allow-print-in-tests
1616
allow-private-module-inception
1717
allow-renamed-params-for
18+
allow-unreachable-in-tests
1819
allow-unwrap-in-consts
1920
allow-unwrap-in-tests
2021
allow-useless-vec-in-tests
@@ -109,6 +110,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
109110
allow-print-in-tests
110111
allow-private-module-inception
111112
allow-renamed-params-for
113+
allow-unreachable-in-tests
112114
allow-unwrap-in-consts
113115
allow-unwrap-in-tests
114116
allow-useless-vec-in-tests
@@ -203,6 +205,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
203205
allow-print-in-tests
204206
allow-private-module-inception
205207
allow-renamed-params-for
208+
allow-unreachable-in-tests
206209
allow-unwrap-in-consts
207210
allow-unwrap-in-tests
208211
allow-useless-vec-in-tests
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)