Skip to content

Commit 8d911fe

Browse files
Heinz GiesLicenser
Heinz Gies
authored andcommitted
add restirction for unreachable and panic
1 parent ee6fc1b commit 8d911fe

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ declare_clippy_lint! {
2525
"missing parameters in `panic!` calls"
2626
}
2727

28+
declare_clippy_lint! {
29+
/// **What it does:** Checks for usage of `panic!`.
30+
///
31+
/// **Why is this bad?** `panic!` will stop the execution of the executable
32+
///
33+
/// **Known problems:** None.
34+
///
35+
/// **Example:**
36+
/// ```no_run
37+
/// panic!("even with a good reason");
38+
/// ```
39+
pub PANIC,
40+
restriction,
41+
"missing parameters in `panic!` calls"
42+
}
43+
2844
declare_clippy_lint! {
2945
/// **What it does:** Checks for usage of `unimplemented!`.
3046
///
@@ -41,7 +57,23 @@ declare_clippy_lint! {
4157
"`unimplemented!` should not be present in production code"
4258
}
4359

44-
declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
60+
declare_clippy_lint! {
61+
/// **What it does:** Checks for usage of `unreachable!`.
62+
///
63+
/// **Why is this bad?** This macro can cause cause code to panics
64+
///
65+
/// **Known problems:** None.
66+
///
67+
/// **Example:**
68+
/// ```no_run
69+
/// unreachable!();
70+
/// ```
71+
pub UNREACHABLE,
72+
restriction,
73+
"`unreachable!` should not be present in production code"
74+
}
75+
76+
declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE]);
4577

4678
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
4779
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
@@ -55,7 +87,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
5587
let span = get_outer_span(expr);
5688
span_lint(cx, UNIMPLEMENTED, span,
5789
"`unimplemented` should not be present in production code");
58-
} else {
90+
} else if is_expn_of(expr.span, "unreachable").is_some() {
91+
let span = get_outer_span(expr);
92+
span_lint(cx, UNREACHABLE, span,
93+
"`unreachable` should not be present in production code");
94+
} else if is_expn_of(expr.span, "panic").is_some() {
95+
let span = get_outer_span(expr);
96+
span_lint(cx, PANIC, span,
97+
"`panic` should not be present in production code");
98+
//} else {
5999
match_panic(params, expr, cx);
60100
}
61101
}

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)