2018-07-28 15:34:52 +00:00
|
|
|
#[allow(clippy::unnecessary_operation)]
|
2021-03-12 14:30:50 +00:00
|
|
|
#[allow(clippy::implicit_clone)]
|
2018-10-10 02:25:03 +00:00
|
|
|
|
2019-08-24 07:23:06 +00:00
|
|
|
fn main() {
|
2018-10-10 02:25:03 +00:00
|
|
|
let x = &Baz;
|
|
|
|
let y = &Baz;
|
|
|
|
y.to_owned() == *x;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this creates an owned instance just for comparison
|
|
|
|
//~| NOTE: `-D clippy::cmp-owned` implied by `-D warnings`
|
2018-10-10 11:51:06 +00:00
|
|
|
|
|
|
|
let x = &&Baz;
|
|
|
|
let y = &Baz;
|
|
|
|
y.to_owned() == **x;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this creates an owned instance just for comparison
|
2022-05-21 11:24:00 +00:00
|
|
|
|
|
|
|
let x = 0u32;
|
|
|
|
let y = U32Wrapper(x);
|
|
|
|
let _ = U32Wrapper::from(x) == y;
|
2017-05-11 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl PartialEq for Foo {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.to_owned() == *other
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this creates an owned instance just for comparison
|
2017-05-11 16:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToOwned for Foo {
|
|
|
|
type Owned = Bar;
|
|
|
|
fn to_owned(&self) -> Bar {
|
|
|
|
Bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 11:24:00 +00:00
|
|
|
#[derive(PartialEq, Eq)]
|
2019-08-24 07:23:06 +00:00
|
|
|
struct Baz;
|
|
|
|
|
|
|
|
impl ToOwned for Baz {
|
|
|
|
type Owned = Baz;
|
|
|
|
fn to_owned(&self) -> Baz {
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 11:24:00 +00:00
|
|
|
#[derive(PartialEq, Eq)]
|
2017-05-11 16:59:36 +00:00
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl PartialEq<Foo> for Bar {
|
|
|
|
fn eq(&self, _: &Foo) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::borrow::Borrow<Foo> for Bar {
|
|
|
|
fn borrow(&self) -> &Foo {
|
|
|
|
static FOO: Foo = Foo;
|
|
|
|
&FOO
|
|
|
|
}
|
2015-05-21 12:51:43 +00:00
|
|
|
}
|
2022-05-21 11:24:00 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
struct U32Wrapper(u32);
|
|
|
|
impl From<u32> for U32Wrapper {
|
|
|
|
fn from(x: u32) -> Self {
|
|
|
|
Self(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl PartialEq<u32> for U32Wrapper {
|
|
|
|
fn eq(&self, other: &u32) -> bool {
|
|
|
|
self.0 == *other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl PartialEq<U32Wrapper> for u32 {
|
|
|
|
fn eq(&self, other: &U32Wrapper) -> bool {
|
|
|
|
*self == other.0
|
|
|
|
}
|
|
|
|
}
|