]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/fallible_impl_from.rs
Rustup
[rust.git] / clippy_lints / src / fallible_impl_from.rs
index e6efd41e6fb9ec94823c9139418cd4f529ef5360..64cdc05b44de33fc9e978fb597885878f49de23d 100644 (file)
@@ -2,8 +2,8 @@
 use rustc::hir;
 use rustc::ty;
 use syntax_pos::Span;
-use utils::{method_chain_args, match_def_path, span_lint_and_then, walk_ptrs_ty};
-use utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
+use crate::utils::{match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty, is_expn_of, opt_def_id};
+use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
 
 /// **What it does:** Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`
 ///
@@ -20,8 +20,9 @@
 ///     }
 /// }
 /// ```
-declare_lint! {
-    pub FALLIBLE_IMPL_FROM, Allow,
+declare_clippy_lint! {
+    pub FALLIBLE_IMPL_FROM,
+    nursery,
     "Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
 }
 
@@ -64,8 +65,10 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
             if_chain! {
                 if let ExprCall(ref func_expr, _) = expr.node;
                 if let ExprPath(QPath::Resolved(_, ref path)) = func_expr.node;
-                if match_def_path(self.tcx, path.def.def_id(), &BEGIN_PANIC) ||
-                    match_def_path(self.tcx, path.def.def_id(), &BEGIN_PANIC_FMT);
+                if let Some(path_def_id) = opt_def_id(path.def);
+                if match_def_path(self.tcx, path_def_id, &BEGIN_PANIC) ||
+                    match_def_path(self.tcx, path_def_id, &BEGIN_PANIC_FMT);
+                if is_expn_of(expr.span, "unreachable").is_none();
                 then {
                     self.result.push(expr.span);
                 }
@@ -74,9 +77,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
             // check for `unwrap`
             if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
                 let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
-                if match_type(self.tcx, reciever_ty, &OPTION) ||
-                    match_type(self.tcx, reciever_ty, &RESULT)
-                {
+                if match_type(self.tcx, reciever_ty, &OPTION) || match_type(self.tcx, reciever_ty, &RESULT) {
                     self.result.push(expr.span);
                 }
             }
@@ -92,7 +93,7 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
 
     for impl_item in impl_items {
         if_chain! {
-            if impl_item.name == "from";
+            if impl_item.ident.name == "from";
             if let ImplItemKind::Method(_, body_id) =
                 cx.tcx.hir.impl_item(impl_item.id).node;
             then {
@@ -105,7 +106,7 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
                     result: Vec::new(),
                 };
                 fpu.visit_expr(&body.value);
-    
+
                 // if we've found one, lint
                 if !fpu.result.is_empty() {
                     span_lint_and_then(