]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/cmp_owned/asymmetric_partial_eq.fixed
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / cmp_owned / asymmetric_partial_eq.fixed
1 // run-rustfix
2 #![allow(unused, clippy::redundant_clone, clippy::derive_partial_eq_without_eq)] // See #5700
3
4 // Define the types in each module to avoid trait impls leaking between modules.
5 macro_rules! impl_types {
6 () => {
7 #[derive(PartialEq)]
8 pub struct Owned;
9
10 pub struct Borrowed;
11
12 impl ToOwned for Borrowed {
13 type Owned = Owned;
14 fn to_owned(&self) -> Owned {
15 Owned {}
16 }
17 }
18
19 impl std::borrow::Borrow<Borrowed> for Owned {
20 fn borrow(&self) -> &Borrowed {
21 static VALUE: Borrowed = Borrowed {};
22 &VALUE
23 }
24 }
25 };
26 }
27
28 // Only Borrowed == Owned is implemented
29 mod borrowed_eq_owned {
30 impl_types!();
31
32 impl PartialEq<Owned> for Borrowed {
33 fn eq(&self, _: &Owned) -> bool {
34 true
35 }
36 }
37
38 pub fn compare() {
39 let owned = Owned {};
40 let borrowed = Borrowed {};
41
42 if borrowed == owned {}
43 if borrowed == owned {}
44 }
45 }
46
47 // Only Owned == Borrowed is implemented
48 mod owned_eq_borrowed {
49 impl_types!();
50
51 impl PartialEq<Borrowed> for Owned {
52 fn eq(&self, _: &Borrowed) -> bool {
53 true
54 }
55 }
56
57 fn compare() {
58 let owned = Owned {};
59 let borrowed = Borrowed {};
60
61 if owned == borrowed {}
62 if owned == borrowed {}
63 }
64 }
65
66 mod issue_4874 {
67 impl_types!();
68
69 // NOTE: PartialEq<Borrowed> for T can't be implemented due to the orphan rules
70 impl<T> PartialEq<T> for Borrowed
71 where
72 T: AsRef<str> + ?Sized,
73 {
74 fn eq(&self, _: &T) -> bool {
75 true
76 }
77 }
78
79 impl std::fmt::Display for Borrowed {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 write!(f, "borrowed")
82 }
83 }
84
85 fn compare() {
86 let borrowed = Borrowed {};
87
88 if borrowed == "Hi" {}
89 if borrowed == "Hi" {}
90 }
91 }
92
93 fn main() {}