]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/clippy_lints/src/manual_unwrap_or.rs
Rollup merge of #85760 - ChrisDenton:path-doc-platform-specific, r=m-ou-se
[rust.git] / src / tools / clippy / clippy_lints / src / manual_unwrap_or.rs
index 520162559e50f74690bcaa67c2f93135dea73f7f..18038dd7819430e004b547a5670b6b8a1a4b0af8 100644 (file)
@@ -1,4 +1,4 @@
-use crate::consts::constant_simple;
+use clippy_utils::consts::constant_simple;
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
 use clippy_utils::ty::is_type_diagnostic_item;
@@ -53,21 +53,6 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
     }
 }
 
-#[derive(Copy, Clone)]
-enum Case {
-    Option,
-    Result,
-}
-
-impl Case {
-    fn unwrap_fn_path(&self) -> &str {
-        match self {
-            Case::Option => "Option::unwrap_or",
-            Case::Result => "Result::unwrap_or",
-        }
-    }
-}
-
 fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
     fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
         if_chain! {
@@ -86,6 +71,7 @@ fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'
             if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
             if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
             if path_to_local_id(unwrap_arm.body, binding_hir_id);
+            if cx.typeck_results().expr_adjustments(unwrap_arm.body).is_empty();
             if !contains_return_break_continue_macro(or_arm.body);
             then {
                 Some(or_arm)
@@ -98,10 +84,10 @@ fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'
     if_chain! {
         if let ExprKind::Match(scrutinee, match_arms, _) = expr.kind;
         let ty = cx.typeck_results().expr_ty(scrutinee);
-        if let Some(case) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
-            Some(Case::Option)
+        if let Some(ty_name) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
+            Some("Option")
         } else if is_type_diagnostic_item(cx, ty, sym::result_type) {
-            Some(Case::Result)
+            Some("Result")
         } else {
             None
         };
@@ -124,7 +110,7 @@ fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'
             span_lint_and_sugg(
                 cx,
                 MANUAL_UNWRAP_OR, expr.span,
-                &format!("this pattern reimplements `{}`", case.unwrap_fn_path()),
+                &format!("this pattern reimplements `{}::unwrap_or`", ty_name),
                 "replace with",
                 format!(
                     "{}.unwrap_or({})",