Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6692,6 +6692,7 @@ Released 2018-09-13
[`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
[`allow-unreachable-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unreachable-in-tests
[`allow-unwrap-in-consts`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unwrap-in-consts
[`allow-unwrap-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-unwrap-in-tests
[`allow-useless-vec-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-useless-vec-in-tests
Expand Down
10 changes: 10 additions & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ default configuration of Clippy. By default, any configuration will replace the
* [`renamed_function_params`](https://rust-lang.github.io/rust-clippy/master/index.html#renamed_function_params)


## `allow-unreachable-in-tests`
Whether `unreachable` should be allowed in test functions or `#[cfg(test)]`

**Default Value:** `false`

---
**Affected lints:**
* [`unreachable`](https://rust-lang.github.io/rust-clippy/master/index.html#unreachable)


## `allow-unwrap-in-consts`
Whether `unwrap` should be allowed in code always evaluated at compile time

Expand Down
3 changes: 3 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ define_Conf! {
#[lints(renamed_function_params)]
allow_renamed_params_for: Vec<String> =
DEFAULT_ALLOWED_TRAITS_WITH_RENAMED_PARAMS.iter().map(ToString::to_string).collect(),
/// Whether `unreachable` should be allowed in test functions or `#[cfg(test)]`
#[lints(unreachable)]
allow_unreachable_in_tests: bool = false,
/// Whether `unwrap` should be allowed in code always evaluated at compile time
#[lints(unwrap_used)]
allow_unwrap_in_consts: bool = true,
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use rustc_span::sym;

pub struct PanicUnimplemented {
allow_panic_in_tests: bool,
allow_unreachable_in_tests: bool,
}

impl PanicUnimplemented {
pub fn new(conf: &'static Conf) -> Self {
Self {
allow_panic_in_tests: conf.allow_panic_in_tests,
allow_unreachable_in_tests: conf.allow_unreachable_in_tests,
}
}
}
Expand Down Expand Up @@ -131,6 +133,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
);
},
Some(sym::unreachable_macro) => {
if self.allow_unreachable_in_tests && is_in_test(cx.tcx, expr.hir_id) {
return;
}

span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
},
_ => {},
Expand Down
3 changes: 3 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
allow-unreachable-in-tests
allow-unwrap-in-consts
allow-unwrap-in-tests
allow-useless-vec-in-tests
Expand Down Expand Up @@ -109,6 +110,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
allow-unreachable-in-tests
allow-unwrap-in-consts
allow-unwrap-in-tests
allow-useless-vec-in-tests
Expand Down Expand Up @@ -203,6 +205,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
allow-print-in-tests
allow-private-module-inception
allow-renamed-params-for
allow-unreachable-in-tests
allow-unwrap-in-consts
allow-unwrap-in-tests
allow-useless-vec-in-tests
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/unreachable/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow-unreachable-in-tests = true
25 changes: 25 additions & 0 deletions tests/ui-toml/unreachable/unreachable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@compile-flags: --test
#![warn(clippy::unreachable)]

fn main() {
unreachable!();
//~^ unreachable
}

#[test]
fn allowed_in_test_fn() {
unreachable!();
}

#[cfg(test)]
mod tests {
#[test]
fn nested_test() {
unreachable!();
}

fn helper() {
// still test context should be allowed
unreachable!();
}
}
11 changes: 11 additions & 0 deletions tests/ui-toml/unreachable/unreachable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: usage of the `unreachable!` macro
--> tests/ui-toml/unreachable/unreachable.rs:5:5
|
LL | unreachable!();
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::unreachable` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`

error: aborting due to 1 previous error