]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #9096 - Jarcho:needless_borrow_subs, r=Manishearth
authorbors <bors@rust-lang.org>
Wed, 6 Jul 2022 21:26:46 +0000 (21:26 +0000)
committerbors <bors@rust-lang.org>
Wed, 6 Jul 2022 21:26:46 +0000 (21:26 +0000)
Fix `needless_borrow` 9095

fixes #9095
changelog: Don't lint `needless_borrow` on method receivers when it would change which trait impl is called

clippy_lints/src/dereference.rs
tests/ui/needless_borrow.fixed
tests/ui/needless_borrow.rs

index 1be1b862066b80418e4acc59ce6a8aaf110eeef6..6dddb969f33400fdaec0a43f13c8bd6973b1decd 100644 (file)
@@ -772,9 +772,14 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
                             } else if let Some(trait_id) = cx.tcx.trait_of_item(id)
                                 && let arg_ty = cx.tcx.erase_regions(cx.typeck_results().expr_ty_adjusted(e))
                                 && let ty::Ref(_, sub_ty, _) = *arg_ty.kind()
-                                && let subs = cx.typeck_results().node_substs_opt(child_id).unwrap_or_else(
-                                    || cx.tcx.mk_substs([].iter())
-                                ) && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() {
+                                && let subs = match cx
+                                    .typeck_results()
+                                    .node_substs_opt(parent.hir_id)
+                                    .and_then(|subs| subs.get(1..))
+                                {
+                                    Some(subs) => cx.tcx.mk_substs(subs.iter().copied()),
+                                    None => cx.tcx.mk_substs([].iter()),
+                                } && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() {
                                     // Trait methods taking `&self`
                                     sub_ty
                                 } else {
index cb005122436019f2a2be9ac1c8fb219d8257e534..09afe2ddbbf6289e21d64cb36e1dea36053b51a0 100644 (file)
@@ -115,6 +115,18 @@ fn main() {
         fn foo_ref(&self) {}
     }
     (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
+
+    struct S;
+    impl From<S> for u32 {
+        fn from(s: S) -> Self {
+            (&s).into()
+        }
+    }
+    impl From<&S> for u32 {
+        fn from(s: &S) -> Self {
+            0
+        }
+    }
 }
 
 #[allow(clippy::needless_borrowed_reference)]
index d636a40100378c0f5bebc5cbb3f85b9f902ab365..3ae4722a1f8985e414ea3a9d49f85ccb397bf770 100644 (file)
@@ -115,6 +115,18 @@ impl FooRef for &'_ () {
         fn foo_ref(&self) {}
     }
     (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
+
+    struct S;
+    impl From<S> for u32 {
+        fn from(s: S) -> Self {
+            (&s).into()
+        }
+    }
+    impl From<&S> for u32 {
+        fn from(s: &S) -> Self {
+            0
+        }
+    }
 }
 
 #[allow(clippy::needless_borrowed_reference)]