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()`
///
/// }
/// }
/// ```
-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()`"
}
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);
}
// 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);
}
}
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 {
result: Vec::new(),
};
fpu.visit_expr(&body.value);
-
+
// if we've found one, lint
if !fpu.result.is_empty() {
span_lint_and_then(