1 use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
2 use crate::utils::{is_expn_of, method_chain_args, span_lint_and_then, walk_ptrs_ty};
3 use if_chain::if_chain;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::ty::{self, Ty};
7 use rustc::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`
13 /// **Why is this bad?** `TryFrom` should be used if there's a possibility of failure.
15 /// **Known problems:** None.
20 /// impl From<String> for Foo {
21 /// fn from(s: String) -> Self {
22 /// Foo(s.parse().unwrap())
26 pub FALLIBLE_IMPL_FROM,
28 "Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
31 declare_lint_pass!(FallibleImplFrom => [FALLIBLE_IMPL_FROM]);
33 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom {
34 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item) {
35 // check for `impl From<???> for ..`
36 let impl_def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
38 if let hir::ItemKind::Impl(.., ref impl_items) = item.node;
39 if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
40 if cx.match_def_path(impl_trait_ref.def_id, &FROM_TRAIT);
42 lint_impl_body(cx, item.span, impl_items);
48 fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_items: &hir::HirVec<hir::ImplItemRef>) {
49 use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
52 struct FindPanicUnwrap<'a, 'tcx: 'a> {
53 lcx: &'a LateContext<'a, 'tcx>,
54 tables: &'tcx ty::TypeckTables<'tcx>,
58 impl<'a, 'tcx: 'a> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
59 fn visit_expr(&mut self, expr: &'tcx Expr) {
60 // check for `begin_panic`
62 if let ExprKind::Call(ref func_expr, _) = expr.node;
63 if let ExprKind::Path(QPath::Resolved(_, ref path)) = func_expr.node;
64 if let Some(path_def_id) = path.res.opt_def_id();
65 if self.lcx.match_def_path(path_def_id, &BEGIN_PANIC) ||
66 self.lcx.match_def_path(path_def_id, &BEGIN_PANIC_FMT);
67 if is_expn_of(expr.span, "unreachable").is_none();
69 self.result.push(expr.span);
74 if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
75 let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
76 if match_type(self.lcx, reciever_ty, &OPTION) || match_type(self.lcx, reciever_ty, &RESULT) {
77 self.result.push(expr.span);
81 // and check sub-expressions
82 intravisit::walk_expr(self, expr);
85 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
86 NestedVisitorMap::None
90 for impl_item in impl_items {
92 if impl_item.ident.name == "from";
93 if let ImplItemKind::Method(_, body_id) =
94 cx.tcx.hir().impl_item(impl_item.id).node;
96 // check the body for `begin_panic` or `unwrap`
97 let body = cx.tcx.hir().body(body_id);
98 let impl_item_def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.id.hir_id);
99 let mut fpu = FindPanicUnwrap {
101 tables: cx.tcx.typeck_tables_of(impl_item_def_id),
104 fpu.visit_expr(&body.value);
106 // if we've found one, lint
107 if !fpu.result.is_empty() {
112 "consider implementing `TryFrom` instead",
115 "`From` is intended for infallible conversions only. \
116 Use `TryFrom` if there's a possibility for the conversion to fail.");
117 db.span_note(fpu.result, "potential failure(s)");
125 fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
127 ty::Adt(adt, _) => cx.match_def_path(adt.did, path),