about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHeinz Gies <hgies@wayfair.com>2019-10-11 13:58:56 +0200
committerHeinz N. Gies <heinz@licenser.net>2019-10-18 07:35:25 +0200
commit8d911fe98839bab2b81291986624419c18ace64a (patch)
tree287a19942b943b294a7e634e71d78a6df8754b7c
parentee6fc1bead1d81dfce866e23d4c5a88770e40cf3 (diff)
downloadrust-8d911fe98839bab2b81291986624419c18ace64a.tar.gz
rust-8d911fe98839bab2b81291986624419c18ace64a.zip
add restirction for unreachable and panic
-rw-r--r--clippy_lints/src/panic_unimplemented.rs44
-rw-r--r--tests/ui/panic_unimplemented.rs9
-rw-r--r--tests/ui/panic_unimplemented.stderr10
3 files changed, 59 insertions, 4 deletions
diff --git a/clippy_lints/src/panic_unimplemented.rs b/clippy_lints/src/panic_unimplemented.rs
index 76200317913..bbb037ad8eb 100644
--- a/clippy_lints/src/panic_unimplemented.rs
+++ b/clippy_lints/src/panic_unimplemented.rs
@@ -26,6 +26,22 @@ declare_clippy_lint! {
 }
 
 declare_clippy_lint! {
+    /// **What it does:** Checks for usage of `panic!`.
+    ///
+    /// **Why is this bad?** `panic!` will stop the execution of the executable
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```no_run
+    /// panic!("even with a good reason");
+    /// ```
+    pub PANIC,
+    restriction,
+    "missing parameters in `panic!` calls"
+}
+
+declare_clippy_lint! {
     /// **What it does:** Checks for usage of `unimplemented!`.
     ///
     /// **Why is this bad?** This macro should not be present in production code
@@ -41,7 +57,23 @@ declare_clippy_lint! {
     "`unimplemented!` should not be present in production code"
 }
 
-declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
+declare_clippy_lint! {
+    /// **What it does:** Checks for usage of `unreachable!`.
+    ///
+    /// **Why is this bad?** This macro can cause cause code to panics
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```no_run
+    /// unreachable!();
+    /// ```
+    pub UNREACHABLE,
+    restriction,
+    "`unreachable!` should not be present in production code"
+}
+
+declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
@@ -55,7 +87,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
                     let span = get_outer_span(expr);
                     span_lint(cx, UNIMPLEMENTED, span,
                               "`unimplemented` should not be present in production code");
-                } else {
+                } else if is_expn_of(expr.span, "unreachable").is_some() {
+                    let span = get_outer_span(expr);
+                    span_lint(cx, UNREACHABLE, span,
+                              "`unreachable` should not be present in production code");
+                } else if is_expn_of(expr.span, "panic").is_some() {
+                    let span = get_outer_span(expr);
+                    span_lint(cx, PANIC, span,
+                              "`panic` should not be present in production code");
+                //} else {
                     match_panic(params, expr, cx);
                 }
             }
diff --git a/tests/ui/panic_unimplemented.rs b/tests/ui/panic_unimplemented.rs
index 92290da8a6a..fed82f13515 100644
--- a/tests/ui/panic_unimplemented.rs
+++ b/tests/ui/panic_unimplemented.rs
@@ -1,4 +1,4 @@
-#![warn(clippy::panic_params, clippy::unimplemented)]
+#![warn(clippy::panic_params, clippy::unimplemented, clippy::unreachable)]
 #![allow(clippy::assertions_on_constants)]
 fn missing() {
     if true {
@@ -56,6 +56,12 @@ fn unimplemented() {
     let b = a + 2;
 }
 
+fn unreachable() {
+    let a = 2;
+    unreachable!();
+    let b = a + 2;
+}
+
 fn main() {
     missing();
     ok_single();
@@ -65,4 +71,5 @@ fn main() {
     ok_nomsg();
     ok_escaped();
     unimplemented();
+    unreachable();
 }
diff --git a/tests/ui/panic_unimplemented.stderr b/tests/ui/panic_unimplemented.stderr
index 588fa187b4a..5f19b35fe6c 100644
--- a/tests/ui/panic_unimplemented.stderr
+++ b/tests/ui/panic_unimplemented.stderr
@@ -32,5 +32,13 @@ LL |     unimplemented!();
    |
    = note: `-D clippy::unimplemented` implied by `-D warnings`
 
-error: aborting due to 5 previous errors
+error: `unreachable` should not be present in production code
+  --> $DIR/panic_unimplemented.rs:61:5
+   |
+LL |     unreachable!();
+   |     ^^^^^^^^^^^^^^^
+   |
+   = note: `-D clippy::unreachable` implied by `-D warnings`
+
+error: aborting due to 6 previous errors