1 use crate::utils::{is_type_diagnostic_item, method_chain_args, return_ty, span_lint_and_then};
2 use if_chain::if_chain;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_middle::hir::map::Map;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::{sym, Span};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for functions of type Result that contain `expect()` or `unwrap()`
13 /// **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.
15 /// **Known problems:** This can cause false positives in functions that handle both recoverable and non recoverable errors.
20 /// fn divisible_by_3(i_str: String) -> Result<(), String> {
23 /// .expect("cannot divide the input by three");
26 /// Err("Number is not divisible by 3")?
35 /// fn divisible_by_3(i_str: String) -> Result<(), String> {
38 /// .map_err(|e| format!("cannot divide the input by three: {}", e))?;
41 /// Err("Number is not divisible by 3")?
49 "functions of type `Result<..>` or `Option`<...> that contain `expect()` or `unwrap()`"
52 declare_lint_pass!(UnwrapInResult=> [UNWRAP_IN_RESULT]);
54 impl<'tcx> LateLintPass<'tcx> for UnwrapInResult {
55 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
57 // first check if it's a method or function
58 if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
59 // checking if its return type is `result` or `option`
60 if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::result_type)
61 || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::option_type);
63 lint_impl_body(cx, impl_item.span, impl_item);
69 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
70 use rustc_hir::{Expr, ImplItemKind};
72 struct FindExpectUnwrap<'a, 'tcx> {
73 lcx: &'a LateContext<'tcx>,
74 typeck_results: &'tcx ty::TypeckResults<'tcx>,
78 impl<'a, 'tcx> Visitor<'tcx> for FindExpectUnwrap<'a, 'tcx> {
81 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
83 if let Some(arglists) = method_chain_args(expr, &["expect"]) {
84 let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
85 if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
86 || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
88 self.result.push(expr.span);
93 if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
94 let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
95 if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
96 || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
98 self.result.push(expr.span);
102 // and check sub-expressions
103 intravisit::walk_expr(self, expr);
106 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
107 NestedVisitorMap::None
111 fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tcx hir::ImplItem<'_>) {
114 if let ImplItemKind::Fn(_, body_id) = impl_item.kind;
116 let body = cx.tcx.hir().body(body_id);
117 let mut fpu = FindExpectUnwrap {
119 typeck_results: cx.tcx.typeck(impl_item.def_id),
122 fpu.visit_expr(&body.value);
124 // if we've found one, lint
125 if !fpu.result.is_empty() {
130 "used unwrap or expect in a function that returns result or option",
133 "unwrap and expect should not be used in a function that returns result or option" );
134 diag.span_note(fpu.result, "potential non-recoverable error(s)");