Skip to content

Commit dc84704

Browse files
author
Heinz Gies
committed
add restirction for unreachable
1 parent 5317efb commit dc84704

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ declare_clippy_lint! {
4242
"`unimplemented!` should not be present in production code"
4343
}
4444

45-
declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
45+
declare_clippy_lint! {
46+
/// **What it does:** Checks for usage of `unreachable!`.
47+
///
48+
/// **Why is this bad?** This macro can cause cause code to panics
49+
///
50+
/// **Known problems:** None.
51+
///
52+
/// **Example:**
53+
/// ```no_run
54+
/// unreachable!();
55+
/// ```
56+
pub UNREACHABLE,
57+
restriction,
58+
"`unreachable!` should not be present in production code"
59+
}
60+
61+
declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE]);
4662

4763
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
4864
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
@@ -59,6 +75,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
5975
let span = get_outer_span(expr);
6076
span_lint(cx, UNIMPLEMENTED, span,
6177
"`unimplemented` should not be present in production code");
78+
} else if is_expn_of(expr.span, "unreachable").is_some() {
79+
let span = get_outer_span(expr);
80+
span_lint(cx, UNREACHABLE, span,
81+
"`unreachable` should not be present in production code");
6282
} else {
6383
match_panic(params, expr, cx);
6484
}

tests/ui/panic_unimplemented.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::panic_params, clippy::unimplemented)]
1+
#![warn(clippy::panic_params, clippy::unimplemented, clippy::unreachable)]
22
#![allow(clippy::assertions_on_constants)]
33
fn missing() {
44
if true {
@@ -56,6 +56,12 @@ fn unimplemented() {
5656
let b = a + 2;
5757
}
5858

59+
fn unreachable() {
60+
let a = 2;
61+
unreachable!();
62+
let b = a + 2;
63+
}
64+
5965
fn main() {
6066
missing();
6167
ok_single();
@@ -65,4 +71,5 @@ fn main() {
6571
ok_nomsg();
6672
ok_escaped();
6773
unimplemented();
74+
unreachable();
6875
}

tests/ui/panic_unimplemented.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@ LL | unimplemented!();
3232
|
3333
= note: `-D clippy::unimplemented` implied by `-D warnings`
3434

35-
error: aborting due to 5 previous errors
35+
error: `unreachable` should not be present in production code
36+
--> $DIR/panic_unimplemented.rs:61:5
37+
|
38+
LL | unreachable!();
39+
| ^^^^^^^^^^^^^^^
40+
|
41+
= note: `-D clippy::unreachable` implied by `-D warnings`
42+
43+
error: aborting due to 6 previous errors
3644

0 commit comments

Comments
 (0)