]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/unwrap_in_result.rs
modify code
[rust.git] / clippy_lints / src / unwrap_in_result.rs
index d17aa6d842411e2ba252c40ea9a58bf793e0fd62..2c13f1049b59955a2e81ebb7b42dc1f5317c1fe3 100644 (file)
@@ -3,22 +3,24 @@
 use clippy_utils::{method_chain_args, return_ty};
 use if_chain::if_chain;
 use rustc_hir as hir;
-use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
+use rustc_hir::intravisit::{self, Visitor};
 use rustc_hir::{Expr, ImplItemKind};
 use rustc_lint::{LateContext, LateLintPass};
-use rustc_middle::hir::map::Map;
 use rustc_middle::ty;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::{sym, Span};
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for functions of type Result that contain `expect()` or `unwrap()`
+    /// ### What it does
+    /// Checks for functions of type `Result` that contain `expect()` or `unwrap()`
     ///
-    /// **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.
+    /// ### 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.
     ///
-    /// **Known problems:** This can cause false positives in functions that handle both recoverable and non recoverable errors.
+    /// ### Known problems
+    /// This can cause false positives in functions that handle both recoverable and non recoverable errors.
     ///
-    /// **Example:**
+    /// ### Example
     /// Before:
     /// ```rust
     /// fn divisible_by_3(i_str: String) -> Result<(), String> {
@@ -48,6 +50,7 @@
     ///     Ok(())
     /// }
     /// ```
+    #[clippy::version = "1.48.0"]
     pub UNWRAP_IN_RESULT,
     restriction,
     "functions of type `Result<..>` or `Option`<...> that contain `expect()` or `unwrap()`"
@@ -61,8 +64,8 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::Impl
             // first check if it's a method or function
             if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
             // checking if its return type is `result` or `option`
-            if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::result_type)
-                || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::option_type);
+            if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Result)
+                || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Option);
             then {
                 lint_impl_body(cx, impl_item.span, impl_item);
             }
@@ -77,14 +80,12 @@ struct FindExpectUnwrap<'a, 'tcx> {
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for FindExpectUnwrap<'a, 'tcx> {
-    type Map = Map<'tcx>;
-
     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
         // check for `expect`
         if let Some(arglists) = method_chain_args(expr, &["expect"]) {
             let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
-            if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
-                || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
+            if is_type_diagnostic_item(self.lcx, reciever_ty, sym::Option)
+                || is_type_diagnostic_item(self.lcx, reciever_ty, sym::Result)
             {
                 self.result.push(expr.span);
             }
@@ -93,8 +94,8 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
         // check for `unwrap`
         if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
             let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
-            if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
-                || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
+            if is_type_diagnostic_item(self.lcx, reciever_ty, sym::Option)
+                || is_type_diagnostic_item(self.lcx, reciever_ty, sym::Result)
             {
                 self.result.push(expr.span);
             }
@@ -103,10 +104,6 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
         // and check sub-expressions
         intravisit::walk_expr(self, expr);
     }
-
-    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
-        NestedVisitorMap::None
-    }
 }
 
 fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tcx hir::ImplItem<'_>) {