]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/unwrap_in_result.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / unwrap_in_result.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::{method_chain_args, return_ty};
4 use if_chain::if_chain;
5 use rustc_hir as hir;
6 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_hir::{Expr, ImplItemKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::hir::map::Map;
10 use rustc_middle::ty;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use rustc_span::{sym, Span};
13
14 declare_clippy_lint! {
15 /// **What it does:** Checks for functions of type Result that contain `expect()` or `unwrap()`
16 ///
17 /// **Why is this bad?** These functions promote recoverable errors to non-recoverable errors which may be undesirable in code bases which wish to avoid panics.
18 ///
19 /// **Known problems:** This can cause false positives in functions that handle both recoverable and non recoverable errors.
20 ///
21 /// **Example:**
22 /// Before:
23 /// ```rust
24 /// fn divisible_by_3(i_str: String) -> Result<(), String> {
25 /// let i = i_str
26 /// .parse::<i32>()
27 /// .expect("cannot divide the input by three");
28 ///
29 /// if i % 3 != 0 {
30 /// Err("Number is not divisible by 3")?
31 /// }
32 ///
33 /// Ok(())
34 /// }
35 /// ```
36 ///
37 /// After:
38 /// ```rust
39 /// fn divisible_by_3(i_str: String) -> Result<(), String> {
40 /// let i = i_str
41 /// .parse::<i32>()
42 /// .map_err(|e| format!("cannot divide the input by three: {}", e))?;
43 ///
44 /// if i % 3 != 0 {
45 /// Err("Number is not divisible by 3")?
46 /// }
47 ///
48 /// Ok(())
49 /// }
50 /// ```
51 pub UNWRAP_IN_RESULT,
52 restriction,
53 "functions of type `Result<..>` or `Option`<...> that contain `expect()` or `unwrap()`"
54 }
55
56 declare_lint_pass!(UnwrapInResult=> [UNWRAP_IN_RESULT]);
57
58 impl<'tcx> LateLintPass<'tcx> for UnwrapInResult {
59 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
60 if_chain! {
61 // first check if it's a method or function
62 if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
63 // checking if its return type is `result` or `option`
64 if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::result_type)
65 || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::option_type);
66 then {
67 lint_impl_body(cx, impl_item.span, impl_item);
68 }
69 }
70 }
71 }
72
73 struct FindExpectUnwrap<'a, 'tcx> {
74 lcx: &'a LateContext<'tcx>,
75 typeck_results: &'tcx ty::TypeckResults<'tcx>,
76 result: Vec<Span>,
77 }
78
79 impl<'a, 'tcx> Visitor<'tcx> for FindExpectUnwrap<'a, 'tcx> {
80 type Map = Map<'tcx>;
81
82 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
83 // check for `expect`
84 if let Some(arglists) = method_chain_args(expr, &["expect"]) {
85 let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
86 if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
87 || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
88 {
89 self.result.push(expr.span);
90 }
91 }
92
93 // check for `unwrap`
94 if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
95 let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
96 if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
97 || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
98 {
99 self.result.push(expr.span);
100 }
101 }
102
103 // and check sub-expressions
104 intravisit::walk_expr(self, expr);
105 }
106
107 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
108 NestedVisitorMap::None
109 }
110 }
111
112 fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tcx hir::ImplItem<'_>) {
113 if let ImplItemKind::Fn(_, body_id) = impl_item.kind {
114 let body = cx.tcx.hir().body(body_id);
115 let mut fpu = FindExpectUnwrap {
116 lcx: cx,
117 typeck_results: cx.tcx.typeck(impl_item.def_id),
118 result: Vec::new(),
119 };
120 fpu.visit_expr(&body.value);
121
122 // if we've found one, lint
123 if !fpu.result.is_empty() {
124 span_lint_and_then(
125 cx,
126 UNWRAP_IN_RESULT,
127 impl_span,
128 "used unwrap or expect in a function that returns result or option",
129 move |diag| {
130 diag.help("unwrap and expect should not be used in a function that returns result or option");
131 diag.span_note(fpu.result, "potential non-recoverable error(s)");
132 },
133 );
134 }
135 }
136 }