1 use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
2 use crate::utils::{is_expn_of, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty};
3 use if_chain::if_chain;
4 use rustc::declare_lint_pass;
5 use rustc::hir::map::Map;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::ty::{self, Ty};
9 use rustc_session::declare_tool_lint;
12 declare_clippy_lint! {
13 /// **What it does:** Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`
15 /// **Why is this bad?** `TryFrom` should be used if there's a possibility of failure.
17 /// **Known problems:** None.
22 /// impl From<String> for Foo {
23 /// fn from(s: String) -> Self {
24 /// Foo(s.parse().unwrap())
28 pub FALLIBLE_IMPL_FROM,
30 "Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
33 declare_lint_pass!(FallibleImplFrom => [FALLIBLE_IMPL_FROM]);
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom {
36 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) {
37 // check for `impl From<???> for ..`
38 let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id);
40 if let hir::ItemKind::Impl(.., impl_items) = item.kind;
41 if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
42 if match_def_path(cx, impl_trait_ref.def_id, &FROM_TRAIT);
44 lint_impl_body(cx, item.span, impl_items);
50 fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_items: &[hir::ImplItemRef<'_>]) {
51 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
54 struct FindPanicUnwrap<'a, 'tcx> {
55 lcx: &'a LateContext<'a, 'tcx>,
56 tables: &'tcx ty::TypeckTables<'tcx>,
60 impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
63 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
64 // check for `begin_panic`
66 if let ExprKind::Call(ref func_expr, _) = expr.kind;
67 if let ExprKind::Path(QPath::Resolved(_, ref path)) = func_expr.kind;
68 if let Some(path_def_id) = path.res.opt_def_id();
69 if match_def_path(self.lcx, path_def_id, &BEGIN_PANIC) ||
70 match_def_path(self.lcx, path_def_id, &BEGIN_PANIC_FMT);
71 if is_expn_of(expr.span, "unreachable").is_none();
73 self.result.push(expr.span);
78 if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
79 let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
80 if match_type(self.lcx, reciever_ty, &OPTION) || match_type(self.lcx, reciever_ty, &RESULT) {
81 self.result.push(expr.span);
85 // and check sub-expressions
86 intravisit::walk_expr(self, expr);
89 fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
90 NestedVisitorMap::None
94 for impl_item in impl_items {
96 if impl_item.ident.name == sym!(from);
97 if let ImplItemKind::Method(_, body_id) =
98 cx.tcx.hir().impl_item(impl_item.id).kind;
100 // check the body for `begin_panic` or `unwrap`
101 let body = cx.tcx.hir().body(body_id);
102 let impl_item_def_id = cx.tcx.hir().local_def_id(impl_item.id.hir_id);
103 let mut fpu = FindPanicUnwrap {
105 tables: cx.tcx.typeck_tables_of(impl_item_def_id),
108 fpu.visit_expr(&body.value);
110 // if we've found one, lint
111 if !fpu.result.is_empty() {
116 "consider implementing `TryFrom` instead",
119 "`From` is intended for infallible conversions only. \
120 Use `TryFrom` if there's a possibility for the conversion to fail.");
121 db.span_note(fpu.result, "potential failure(s)");
129 fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
131 ty::Adt(adt, _) => match_def_path(cx, adt.did, path),