diff --git a/tests/ui/binop/binary-op-suggest-deref.fixed b/tests/ui/binop/binary-op-suggest-deref.fixed deleted file mode 100644 index 1ff3599137b16..0000000000000 --- a/tests/ui/binop/binary-op-suggest-deref.fixed +++ /dev/null @@ -1,8 +0,0 @@ -// Issue #52544 -// run-rustfix - -fn main() { - let i: &i64 = &1; - if *i < 0 {} - //~^ ERROR mismatched types [E0308] -} diff --git a/tests/ui/binop/binary-op-suggest-deref.rs b/tests/ui/binop/binary-op-suggest-deref.rs index 12505a9ac27e8..57f24a4c28ed4 100644 --- a/tests/ui/binop/binary-op-suggest-deref.rs +++ b/tests/ui/binop/binary-op-suggest-deref.rs @@ -1,8 +1,75 @@ -// Issue #52544 -// run-rustfix +#![allow(dead_code)] -fn main() { +fn foo() { + // Issue #52544 let i: &i64 = &1; if i < 0 {} //~^ ERROR mismatched types [E0308] } + +fn bar() { + // Issue #40660 + let foo = &&0; + + // Dereference LHS + _ = foo == 0; + //~^ERROR can't compare `&&{integer}` with `{integer}` [E0277] + _ = foo == &0; + //~^ERROR can't compare `&{integer}` with `{integer}` [E0277] + _ = &&&&foo == 0; + //~^ERROR can't compare `&&&&&&{integer}` with `{integer}` [E0277] + _ = *foo == 0; + //~^ERROR can't compare `&{integer}` with `{integer}` [E0277] + _ = &&foo == &&0; + //~^ERROR can't compare `&&{integer}` with `{integer}` [E0277] + _ = &Box::new(42) == 42; + //~^ERROR can't compare `&Box<{integer}>` with `{integer}` [E0277] + _ = &Box::new(&Box::new(&42)) == 42; + //~^ERROR can't compare `&Box<&Box<&{integer}>>` with `{integer}` [E0277] + + // Dereference RHS + _ = 0 == foo; + //~^ERROR can't compare `{integer}` with `&&{integer}` [E0277] + _ = &0 == foo; + //~^ERROR can't compare `{integer}` with `&{integer}` [E0277] + _ = 0 == &&&&foo; + //~^ERROR can't compare `{integer}` with `&&&&&&{integer}` [E0277] + _ = 0 == *foo; + //~^ERROR can't compare `{integer}` with `&{integer}` [E0277] + _ = &&0 == &&foo; + //~^ERROR can't compare `{integer}` with `&&{integer}` [E0277] + + // Dereference both sides + _ = &Box::new(Box::new(42)) == &foo; + //~^ERROR can't compare `Box>` with `&&{integer}` [E0277] + _ = &Box::new(42) == &foo; + //~^ERROR can't compare `Box<{integer}>` with `&&{integer}` [E0277] + _ = &Box::new(Box::new(Box::new(Box::new(42)))) == &foo; + //~^ERROR can't compare `Box>>>` with `&&{integer}` [E0277] + _ = &foo == &Box::new(Box::new(Box::new(Box::new(42)))); + //~^ERROR can't compare `&&{integer}` with `Box>>>` [E0277] + + // Don't suggest dereferencing the LHS; suggest boxing the RHS instead + _ = Box::new(42) == 42; + //~^ERROR mismatched types [E0308] + + // Don't suggest dereferencing with types that can't be compared + struct Foo; + _ = &&0 == Foo; + //~^ERROR can't compare `&&{integer}` with `Foo` [E0277] + _ = Foo == &&0; + //~^ERROR binary operation `==` cannot be applied to type `Foo` [E0369] +} + +fn baz() { + // Issue #44695 + let owned = "foo".to_owned(); + let string_ref = &owned; + let partial = "foobar"; + _ = string_ref == partial[..3]; + //~^ERROR can't compare `&String` with `str` [E0277] + _ = partial[..3] == string_ref; + //~^ERROR can't compare `str` with `&String` [E0277] +} + +fn main() {} diff --git a/tests/ui/binop/binary-op-suggest-deref.stderr b/tests/ui/binop/binary-op-suggest-deref.stderr index d1d0089ece795..c2b9bea5eeec5 100644 --- a/tests/ui/binop/binary-op-suggest-deref.stderr +++ b/tests/ui/binop/binary-op-suggest-deref.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/binary-op-suggest-deref.rs:6:12 + --> $DIR/binary-op-suggest-deref.rs:8:12 | LL | if i < 0 {} | ^ expected `&i64`, found integer @@ -9,6 +9,430 @@ help: consider dereferencing the borrow LL | if *i < 0 {} | + -error: aborting due to 1 previous error +error[E0277]: can't compare `&&{integer}` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:17:13 + | +LL | _ = foo == 0; + | ^^ no implementation for `&&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider dereferencing here + | +LL | _ = **foo == 0; + | ++ + +error[E0277]: can't compare `&{integer}` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:19:13 + | +LL | _ = foo == &0; + | ^^ no implementation for `&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + = note: required for `&&{integer}` to implement `PartialEq<&{integer}>` +help: consider dereferencing here + | +LL | _ = *foo == &0; + | + + +error[E0277]: can't compare `&&&&&&{integer}` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:21:17 + | +LL | _ = &&&&foo == 0; + | ^^ no implementation for `&&&&&&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&&&&&&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider removing the borrows and dereferencing instead + | +LL - _ = &&&&foo == 0; +LL + _ = **foo == 0; + | + +error[E0277]: can't compare `&{integer}` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:23:14 + | +LL | _ = *foo == 0; + | ^^ no implementation for `&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider dereferencing here + | +LL | _ = **foo == 0; + | + + +error[E0277]: can't compare `&&{integer}` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:25:15 + | +LL | _ = &&foo == &&0; + | ^^ no implementation for `&&{integer} == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + = note: required for `&&&{integer}` to implement `PartialEq<&{integer}>` + = note: 1 redundant requirement hidden + = note: required for `&&&&{integer}` to implement `PartialEq<&&{integer}>` +help: consider removing the borrows + | +LL - _ = &&foo == &&0; +LL + _ = foo == &&0; + | + +error[E0277]: can't compare `&Box<{integer}>` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:27:23 + | +LL | _ = &Box::new(42) == 42; + | ^^ no implementation for `&Box<{integer}> == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&Box<{integer}>` + = help: the trait `PartialEq` is implemented for `Box` +help: consider removing the borrow and dereferencing instead + | +LL - _ = &Box::new(42) == 42; +LL + _ = *Box::new(42) == 42; + | + +error[E0277]: can't compare `&Box<&Box<&{integer}>>` with `{integer}` + --> $DIR/binary-op-suggest-deref.rs:29:35 + | +LL | _ = &Box::new(&Box::new(&42)) == 42; + | ^^ no implementation for `&Box<&Box<&{integer}>> == {integer}` + | + = help: the trait `PartialEq<{integer}>` is not implemented for `&Box<&Box<&{integer}>>` + = help: the trait `PartialEq` is implemented for `Box` +help: consider removing the borrow and dereferencing instead + | +LL - _ = &Box::new(&Box::new(&42)) == 42; +LL + _ = ****Box::new(&Box::new(&42)) == 42; + | + +error[E0277]: can't compare `{integer}` with `&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:33:11 + | +LL | _ = 0 == foo; + | ^^ no implementation for `{integer} == &&{integer}` + | + = help: the trait `PartialEq<&&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider dereferencing here + | +LL | _ = 0 == **foo; + | ++ + +error[E0277]: can't compare `{integer}` with `&{integer}` + --> $DIR/binary-op-suggest-deref.rs:35:12 + | +LL | _ = &0 == foo; + | ^^ no implementation for `{integer} == &{integer}` + | + = help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + = note: required for `&{integer}` to implement `PartialEq<&&{integer}>` +help: consider dereferencing here + | +LL | _ = &0 == *foo; + | + + +error[E0277]: can't compare `{integer}` with `&&&&&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:37:11 + | +LL | _ = 0 == &&&&foo; + | ^^ no implementation for `{integer} == &&&&&&{integer}` + | + = help: the trait `PartialEq<&&&&&&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider removing the borrows and dereferencing instead + | +LL - _ = 0 == &&&&foo; +LL + _ = 0 == **foo; + | + +error[E0277]: can't compare `{integer}` with `&{integer}` + --> $DIR/binary-op-suggest-deref.rs:39:11 + | +LL | _ = 0 == *foo; + | ^^ no implementation for `{integer} == &{integer}` + | + = help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others +help: consider dereferencing here + | +LL | _ = 0 == **foo; + | + + +error[E0277]: can't compare `{integer}` with `&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:41:13 + | +LL | _ = &&0 == &&foo; + | ^^ no implementation for `{integer} == &&{integer}` + | + = help: the trait `PartialEq<&&{integer}>` is not implemented for `{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + = note: required for `&{integer}` to implement `PartialEq<&&&{integer}>` + = note: 1 redundant requirement hidden + = note: required for `&&{integer}` to implement `PartialEq<&&&&{integer}>` +help: consider removing the borrows + | +LL - _ = &&0 == &&foo; +LL + _ = &&0 == foo; + | + +error[E0277]: can't compare `Box>` with `&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:45:33 + | +LL | _ = &Box::new(Box::new(42)) == &foo; + | ^^ no implementation for `Box> == &&{integer}` + | + = help: the trait `PartialEq<&&{integer}>` is not implemented for `Box>` + = help: the trait `PartialEq` is implemented for `Box>` + = help: for that trait implementation, expected `Box>`, found `&&{integer}` + = note: required for `&Box>` to implement `PartialEq<&&&{integer}>` +help: consider dereferencing both sides + | +LL - _ = &Box::new(Box::new(42)) == &foo; +LL + _ = **Box::new(Box::new(42)) == **foo; + | + +error[E0277]: can't compare `Box<{integer}>` with `&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:47:23 + | +LL | _ = &Box::new(42) == &foo; + | ^^ no implementation for `Box<{integer}> == &&{integer}` + | + = help: the trait `PartialEq<&&{integer}>` is not implemented for `Box<{integer}>` + = help: the trait `PartialEq` is implemented for `Box<{integer}>` + = help: for that trait implementation, expected `Box<{integer}>`, found `&&{integer}` + = note: required for `&Box<{integer}>` to implement `PartialEq<&&&{integer}>` +help: consider dereferencing both sides + | +LL - _ = &Box::new(42) == &foo; +LL + _ = *Box::new(42) == **foo; + | + +error[E0277]: can't compare `Box>>>` with `&&{integer}` + --> $DIR/binary-op-suggest-deref.rs:49:53 + | +LL | _ = &Box::new(Box::new(Box::new(Box::new(42)))) == &foo; + | ^^ no implementation for `Box>>> == &&{integer}` + | + = help: the trait `PartialEq<&&{integer}>` is not implemented for `Box>>>` + = help: the trait `PartialEq` is implemented for `Box>>>` + = help: for that trait implementation, expected `Box>>>`, found `&&{integer}` + = note: required for `&Box>>>` to implement `PartialEq<&&&{integer}>` +help: consider dereferencing both sides + | +LL - _ = &Box::new(Box::new(Box::new(Box::new(42)))) == &foo; +LL + _ = ****Box::new(Box::new(Box::new(Box::new(42)))) == **foo; + | + +error[E0277]: can't compare `&&{integer}` with `Box>>>` + --> $DIR/binary-op-suggest-deref.rs:51:14 + | +LL | _ = &foo == &Box::new(Box::new(Box::new(Box::new(42)))); + | ^^ no implementation for `&&{integer} == Box>>>` + | + = help: the trait `PartialEq>>>>` is not implemented for `&&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + = note: required for `&&&{integer}` to implement `PartialEq<&Box>>>>` +help: consider dereferencing both sides + | +LL - _ = &foo == &Box::new(Box::new(Box::new(Box::new(42)))); +LL + _ = **foo == ****Box::new(Box::new(Box::new(Box::new(42)))); + | + +error[E0308]: mismatched types + --> $DIR/binary-op-suggest-deref.rs:55:25 + | +LL | _ = Box::new(42) == 42; + | ------------ ^^ expected `Box<{integer}>`, found integer + | | + | expected because this is `Box<{integer}>` + | + = note: expected struct `Box<{integer}>` + found type `{integer}` + = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html +help: store this in the heap by calling `Box::new` + | +LL | _ = Box::new(42) == Box::new(42); + | +++++++++ + + +error[E0277]: can't compare `&&{integer}` with `Foo` + --> $DIR/binary-op-suggest-deref.rs:60:13 + | +LL | _ = &&0 == Foo; + | ^^ no implementation for `&&{integer} == Foo` + | + = help: the trait `PartialEq` is not implemented for `&&{integer}` + = help: the following other types implement trait `PartialEq`: + isize + i8 + i16 + i32 + i64 + i128 + usize + u8 + and 6 others + +error[E0369]: binary operation `==` cannot be applied to type `Foo` + --> $DIR/binary-op-suggest-deref.rs:62:13 + | +LL | _ = Foo == &&0; + | --- ^^ --- &&{integer} + | | + | Foo + | +note: an implementation of `PartialEq<&&{integer}>` might be missing for `Foo` + --> $DIR/binary-op-suggest-deref.rs:59:5 + | +LL | struct Foo; + | ^^^^^^^^^^ must implement `PartialEq<&&{integer}>` +help: consider annotating `Foo` with `#[derive(PartialEq)]` + | +LL + #[derive(PartialEq)] +LL | struct Foo; + | + +error[E0277]: can't compare `&String` with `str` + --> $DIR/binary-op-suggest-deref.rs:71:20 + | +LL | _ = string_ref == partial[..3]; + | ^^ no implementation for `&String == str` + | + = help: the trait `PartialEq` is not implemented for `&String` + = help: the following other types implement trait `PartialEq`: + >> + + > + > +help: consider dereferencing here + | +LL | _ = *string_ref == partial[..3]; + | + + +error[E0277]: can't compare `str` with `&String` + --> $DIR/binary-op-suggest-deref.rs:73:22 + | +LL | _ = partial[..3] == string_ref; + | ^^ no implementation for `str == &String` + | + = help: the trait `PartialEq<&String>` is not implemented for `str` + = help: the following other types implement trait `PartialEq`: + >> + > + > + > + + <&'a str as PartialEq> + <&'a str as PartialEq> + <&'b str as PartialEq>> +help: consider dereferencing here + | +LL | _ = partial[..3] == *string_ref; + | + + +error: aborting due to 22 previous errors -For more information about this error, try `rustc --explain E0308`. +Some errors have detailed explanations: E0277, E0308, E0369. +For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/dst/issue-113447.fixed b/tests/ui/dst/issue-113447.fixed deleted file mode 100644 index 536f680f697c7..0000000000000 --- a/tests/ui/dst/issue-113447.fixed +++ /dev/null @@ -1,25 +0,0 @@ -// run-rustfix - -pub struct Bytes; - -impl Bytes { - pub fn as_slice(&self) -> &[u8] { - todo!() - } -} - -impl PartialEq<[u8]> for Bytes { - fn eq(&self, other: &[u8]) -> bool { - self.as_slice() == other - } -} - -impl PartialEq for &[u8] { - fn eq(&self, other: &Bytes) -> bool { - *other == **self - } -} - -fn main() { - let _ = &[0u8] == &[0xAA][..]; //~ ERROR can't compare `&[u8; 1]` with `[{integer}; 1]` -} diff --git a/tests/ui/dst/issue-113447.rs b/tests/ui/dst/issue-113447.rs index c10a4f2ff8ec4..75156a117e996 100644 --- a/tests/ui/dst/issue-113447.rs +++ b/tests/ui/dst/issue-113447.rs @@ -1,5 +1,3 @@ -// run-rustfix - pub struct Bytes; impl Bytes { diff --git a/tests/ui/dst/issue-113447.stderr b/tests/ui/dst/issue-113447.stderr index 266eb228046a2..19da251e99a3a 100644 --- a/tests/ui/dst/issue-113447.stderr +++ b/tests/ui/dst/issue-113447.stderr @@ -15,6 +15,11 @@ LL | let _ = &[0u8] == [0xAA]; <[B] as PartialEq<[A; N]>> <&[u8] as PartialEq> and 4 others +help: consider removing the borrow + | +LL - let _ = &[0u8] == [0xAA]; +LL + let _ = [0u8] == [0xAA]; + | help: convert the array to a `&[u8]` slice instead | LL | let _ = &[0u8] == &[0xAA][..]; diff --git a/tests/ui/partialeq_help.stderr b/tests/ui/partialeq_help.stderr index fdff94f425c8a..f5de1308e8714 100644 --- a/tests/ui/partialeq_help.stderr +++ b/tests/ui/partialeq_help.stderr @@ -5,6 +5,10 @@ LL | a == b; | ^^ no implementation for `&T == T` | = help: the trait `PartialEq` is not implemented for `&T` +help: consider dereferencing here + | +LL | *a == b; + | + help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement | LL | fn foo(a: &T, b: T) where &T: PartialEq { @@ -17,6 +21,10 @@ LL | a == b; | ^^ no implementation for `&T == T` | = help: the trait `PartialEq` is not implemented for `&T` +help: consider dereferencing here + | +LL | *a == b; + | + help: consider extending the `where` clause, but there might be an alternative better way to express this requirement | LL | fn foo2(a: &T, b: T) where &T: PartialEq {