about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-17 17:13:57 +0000
committerbors <bors@rust-lang.org>2021-05-17 17:13:57 +0000
commitcec8d957291644ded59b29a08414112e8a3c42ec (patch)
tree25eabcac52e95857d04c4db65f8acd1ef45977ae
parentacdf43f2575d534893840091c6c0a19b861ab263 (diff)
parentebf065e5172227a99743f4032c5bfff80d5630e6 (diff)
downloadrust-cec8d957291644ded59b29a08414112e8a3c42ec.tar.gz
rust-cec8d957291644ded59b29a08414112e8a3c42ec.zip
Auto merge of #7233 - giraffate:fix_manual_unwrap_or_fp_with_deref_coercion, r=flip1995
Fix a `manual_unwrap_or` FP with deref coercion

Fix https://github.com/rust-lang/rust-clippy/issues/7228.

changelog: Fix a [`manual_unwrap_or`] FP with deref coercion
-rw-r--r--clippy_lints/src/manual_unwrap_or.rs3
-rw-r--r--tests/ui/manual_unwrap_or.fixed8
-rw-r--r--tests/ui/manual_unwrap_or.rs8
3 files changed, 19 insertions, 0 deletions
diff --git a/clippy_lints/src/manual_unwrap_or.rs b/clippy_lints/src/manual_unwrap_or.rs
index 520162559e5..cf183d4c16f 100644
--- a/clippy_lints/src/manual_unwrap_or.rs
+++ b/clippy_lints/src/manual_unwrap_or.rs
@@ -11,6 +11,7 @@ use rustc_hir::{Arm, Expr, ExprKind, PatKind};
 use rustc_lint::LintContext;
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::lint::in_external_macro;
+use rustc_middle::ty::adjustment::Adjust;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::sym;
 
@@ -87,6 +88,8 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
             if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
             if path_to_local_id(unwrap_arm.body, binding_hir_id);
             if !contains_return_break_continue_macro(or_arm.body);
+            if !cx.typeck_results().expr_adjustments(unwrap_arm.body).iter()
+                .any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
             then {
                 Some(or_arm)
             } else {
diff --git a/tests/ui/manual_unwrap_or.fixed b/tests/ui/manual_unwrap_or.fixed
index e7a29596b73..1efecb0ba12 100644
--- a/tests/ui/manual_unwrap_or.fixed
+++ b/tests/ui/manual_unwrap_or.fixed
@@ -163,4 +163,12 @@ mod issue6965 {
     }
 }
 
+use std::rc::Rc;
+fn format_name(name: Option<&Rc<str>>) -> &str {
+    match name {
+        None => "<anon>",
+        Some(name) => name,
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/manual_unwrap_or.rs b/tests/ui/manual_unwrap_or.rs
index 66006b6c616..95d585ad18a 100644
--- a/tests/ui/manual_unwrap_or.rs
+++ b/tests/ui/manual_unwrap_or.rs
@@ -205,4 +205,12 @@ mod issue6965 {
     }
 }
 
+use std::rc::Rc;
+fn format_name(name: Option<&Rc<str>>) -> &str {
+    match name {
+        None => "<anon>",
+        Some(name) => name,
+    }
+}
+
 fn main() {}