-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5701 - ebroto:4874_cmp_owned_fp, r=flip1995
cmp_owned: handle when PartialEq is not implemented symmetrically changelog: Handle asymmetrical implementations of PartialEq in [`cmp_owned`]. Fixes #4874
- Loading branch information
Showing
4 changed files
with
291 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// run-rustfix | ||
#![allow(unused, clippy::redundant_clone)] // See #5700 | ||
|
||
// Define the types in each module to avoid trait impls leaking between modules. | ||
macro_rules! impl_types { | ||
() => { | ||
#[derive(PartialEq)] | ||
pub struct Owned; | ||
|
||
pub struct Borrowed; | ||
|
||
impl ToOwned for Borrowed { | ||
type Owned = Owned; | ||
fn to_owned(&self) -> Owned { | ||
Owned {} | ||
} | ||
} | ||
|
||
impl std::borrow::Borrow<Borrowed> for Owned { | ||
fn borrow(&self) -> &Borrowed { | ||
static VALUE: Borrowed = Borrowed {}; | ||
&VALUE | ||
} | ||
} | ||
}; | ||
} | ||
|
||
// Only Borrowed == Owned is implemented | ||
mod borrowed_eq_owned { | ||
impl_types!(); | ||
|
||
impl PartialEq<Owned> for Borrowed { | ||
fn eq(&self, _: &Owned) -> bool { | ||
true | ||
} | ||
} | ||
|
||
pub fn compare() { | ||
let owned = Owned {}; | ||
let borrowed = Borrowed {}; | ||
|
||
if borrowed == owned {} | ||
if borrowed == owned {} | ||
} | ||
} | ||
|
||
// Only Owned == Borrowed is implemented | ||
mod owned_eq_borrowed { | ||
impl_types!(); | ||
|
||
impl PartialEq<Borrowed> for Owned { | ||
fn eq(&self, _: &Borrowed) -> bool { | ||
true | ||
} | ||
} | ||
|
||
fn compare() { | ||
let owned = Owned {}; | ||
let borrowed = Borrowed {}; | ||
|
||
if owned == borrowed {} | ||
if owned == borrowed {} | ||
} | ||
} | ||
|
||
mod issue_4874 { | ||
impl_types!(); | ||
|
||
// NOTE: PartialEq<Borrowed> for T can't be implemented due to the orphan rules | ||
impl<T> PartialEq<T> for Borrowed | ||
where | ||
T: AsRef<str> + ?Sized, | ||
{ | ||
fn eq(&self, _: &T) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Borrowed { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "borrowed") | ||
} | ||
} | ||
|
||
fn compare() { | ||
let borrowed = Borrowed {}; | ||
|
||
if borrowed == "Hi" {} | ||
if borrowed == "Hi" {} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// run-rustfix | ||
#![allow(unused, clippy::redundant_clone)] // See #5700 | ||
|
||
// Define the types in each module to avoid trait impls leaking between modules. | ||
macro_rules! impl_types { | ||
() => { | ||
#[derive(PartialEq)] | ||
pub struct Owned; | ||
|
||
pub struct Borrowed; | ||
|
||
impl ToOwned for Borrowed { | ||
type Owned = Owned; | ||
fn to_owned(&self) -> Owned { | ||
Owned {} | ||
} | ||
} | ||
|
||
impl std::borrow::Borrow<Borrowed> for Owned { | ||
fn borrow(&self) -> &Borrowed { | ||
static VALUE: Borrowed = Borrowed {}; | ||
&VALUE | ||
} | ||
} | ||
}; | ||
} | ||
|
||
// Only Borrowed == Owned is implemented | ||
mod borrowed_eq_owned { | ||
impl_types!(); | ||
|
||
impl PartialEq<Owned> for Borrowed { | ||
fn eq(&self, _: &Owned) -> bool { | ||
true | ||
} | ||
} | ||
|
||
pub fn compare() { | ||
let owned = Owned {}; | ||
let borrowed = Borrowed {}; | ||
|
||
if borrowed.to_owned() == owned {} | ||
if owned == borrowed.to_owned() {} | ||
} | ||
} | ||
|
||
// Only Owned == Borrowed is implemented | ||
mod owned_eq_borrowed { | ||
impl_types!(); | ||
|
||
impl PartialEq<Borrowed> for Owned { | ||
fn eq(&self, _: &Borrowed) -> bool { | ||
true | ||
} | ||
} | ||
|
||
fn compare() { | ||
let owned = Owned {}; | ||
let borrowed = Borrowed {}; | ||
|
||
if owned == borrowed.to_owned() {} | ||
if borrowed.to_owned() == owned {} | ||
} | ||
} | ||
|
||
mod issue_4874 { | ||
impl_types!(); | ||
|
||
// NOTE: PartialEq<Borrowed> for T can't be implemented due to the orphan rules | ||
impl<T> PartialEq<T> for Borrowed | ||
where | ||
T: AsRef<str> + ?Sized, | ||
{ | ||
fn eq(&self, _: &T) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Borrowed { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "borrowed") | ||
} | ||
} | ||
|
||
fn compare() { | ||
let borrowed = Borrowed {}; | ||
|
||
if "Hi" == borrowed.to_string() {} | ||
if borrowed.to_string() == "Hi" {} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:42:12 | ||
| | ||
LL | if borrowed.to_owned() == owned {} | ||
| ^^^^^^^^^^^^^^^^^^^ help: try: `borrowed` | ||
| | ||
= note: `-D clippy::cmp-owned` implied by `-D warnings` | ||
|
||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:43:21 | ||
| | ||
LL | if owned == borrowed.to_owned() {} | ||
| ---------^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `borrowed == owned` | ||
|
||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:61:21 | ||
| | ||
LL | if owned == borrowed.to_owned() {} | ||
| ^^^^^^^^^^^^^^^^^^^ help: try: `borrowed` | ||
|
||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:62:12 | ||
| | ||
LL | if borrowed.to_owned() == owned {} | ||
| ^^^^^^^^^^^^^^^^^^^--------- | ||
| | | ||
| help: try: `owned == borrowed` | ||
|
||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:88:20 | ||
| | ||
LL | if "Hi" == borrowed.to_string() {} | ||
| --------^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try: `borrowed == "Hi"` | ||
|
||
error: this creates an owned instance just for comparison | ||
--> $DIR/asymmetric_partial_eq.rs:89:12 | ||
| | ||
LL | if borrowed.to_string() == "Hi" {} | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `borrowed` | ||
|
||
error: aborting due to 6 previous errors | ||
|