Skip to content

Commit 0a87173

Browse files
Heinz GiesLicenser
authored andcommitted
add restirction for unreachable
1 parent 55e7818 commit 0a87173

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
@@ -41,7 +41,23 @@ declare_clippy_lint! {
4141
"`unimplemented!` should not be present in production code"
4242
}
4343

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

4662
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
4763
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
@@ -55,6 +71,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
5571
let span = get_outer_span(expr);
5672
span_lint(cx, UNIMPLEMENTED, span,
5773
"`unimplemented` should not be present in production code");
74+
} else if is_expn_of(expr.span, "unreachable").is_some() {
75+
let span = get_outer_span(expr);
76+
span_lint(cx, UNREACHABLE, span,
77+
"`unreachable` should not be present in production code");
5878
} else {
5979
match_panic(params, expr, cx);
6080
}

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)