New lint: [derive_partial_eq_without_eq]
This commit is contained in:
parent
77effb7bb2
commit
fe84ff3360
29 changed files with 359 additions and 38 deletions
|
|
@ -37,7 +37,7 @@ fn main() {
|
|||
|
||||
use std::cmp::{Ordering, PartialEq, PartialOrd};
|
||||
|
||||
#[derive(PartialEq, PartialOrd)]
|
||||
#[derive(PartialEq, Eq, PartialOrd)]
|
||||
pub struct U(u64);
|
||||
|
||||
impl PartialEq<u32> for U {
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ fn main() {
|
|||
|
||||
use std::ops::{Mul, MulAssign};
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Wrap(i64);
|
||||
|
||||
impl Mul<i64> for Wrap {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-rustfix
|
||||
#![allow(unused, clippy::redundant_clone)] // See #5700
|
||||
#![allow(unused, clippy::redundant_clone, clippy::derive_partial_eq_without_eq)] // See #5700
|
||||
|
||||
// Define the types in each module to avoid trait impls leaking between modules.
|
||||
macro_rules! impl_types {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-rustfix
|
||||
#![allow(unused, clippy::redundant_clone)] // See #5700
|
||||
#![allow(unused, clippy::redundant_clone, clippy::derive_partial_eq_without_eq)] // See #5700
|
||||
|
||||
// Define the types in each module to avoid trait impls leaking between modules.
|
||||
macro_rules! impl_types {
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ impl ToOwned for Foo {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Bar;
|
||||
|
||||
impl PartialEq<Foo> for Bar {
|
||||
|
|
@ -61,7 +61,7 @@ impl std::borrow::Borrow<Foo> for Bar {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Baz;
|
||||
|
||||
impl ToOwned for Baz {
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ impl ToOwned for Foo {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Bar;
|
||||
|
||||
impl PartialEq<Foo> for Bar {
|
||||
|
|
@ -61,7 +61,7 @@ impl std::borrow::Borrow<Foo> for Bar {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Baz;
|
||||
|
||||
impl ToOwned for Baz {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ impl ToOwned for Foo {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Baz;
|
||||
|
||||
impl ToOwned for Baz {
|
||||
|
|
@ -36,7 +36,7 @@ impl ToOwned for Baz {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct Bar;
|
||||
|
||||
impl PartialEq<Foo> for Bar {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// panicked at 'assertion failed: rows.iter().all(|r| r.len() == v.len())',
|
||||
// compiler/rustc_mir_build/src/thir/pattern/_match.rs:2030:5
|
||||
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(PartialEq)]
|
||||
struct Foo(i32);
|
||||
const FOO_REF_REF: &&Foo = &&Foo(42);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
|
||||
--> $DIR/ice-6254.rs:12:9
|
||||
--> $DIR/ice-6254.rs:13:9
|
||||
|
|
||||
LL | FOO_REF_REF => {},
|
||||
| ^^^^^^^^^^^
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
#[derive(PartialEq, Hash)]
|
||||
struct Foo;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
error: you are deriving `Hash` but have implemented `PartialEq` explicitly
|
||||
--> $DIR/derive_hash_xor_eq.rs:10:10
|
||||
--> $DIR/derive_hash_xor_eq.rs:12:10
|
||||
|
|
||||
LL | #[derive(Hash)]
|
||||
| ^^^^
|
||||
|
|
||||
= note: `#[deny(clippy::derive_hash_xor_eq)]` on by default
|
||||
note: `PartialEq` implemented here
|
||||
--> $DIR/derive_hash_xor_eq.rs:13:1
|
||||
--> $DIR/derive_hash_xor_eq.rs:15:1
|
||||
|
|
||||
LL | / impl PartialEq for Bar {
|
||||
LL | | fn eq(&self, _: &Bar) -> bool {
|
||||
|
|
@ -17,13 +17,13 @@ LL | | }
|
|||
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: you are deriving `Hash` but have implemented `PartialEq` explicitly
|
||||
--> $DIR/derive_hash_xor_eq.rs:19:10
|
||||
--> $DIR/derive_hash_xor_eq.rs:21:10
|
||||
|
|
||||
LL | #[derive(Hash)]
|
||||
| ^^^^
|
||||
|
|
||||
note: `PartialEq` implemented here
|
||||
--> $DIR/derive_hash_xor_eq.rs:22:1
|
||||
--> $DIR/derive_hash_xor_eq.rs:24:1
|
||||
|
|
||||
LL | / impl PartialEq<Baz> for Baz {
|
||||
LL | | fn eq(&self, _: &Baz) -> bool {
|
||||
|
|
@ -34,7 +34,7 @@ LL | | }
|
|||
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: you are implementing `Hash` explicitly but have derived `PartialEq`
|
||||
--> $DIR/derive_hash_xor_eq.rs:31:1
|
||||
--> $DIR/derive_hash_xor_eq.rs:33:1
|
||||
|
|
||||
LL | / impl std::hash::Hash for Bah {
|
||||
LL | | fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
|
||||
|
|
@ -42,14 +42,14 @@ LL | | }
|
|||
| |_^
|
||||
|
|
||||
note: `PartialEq` implemented here
|
||||
--> $DIR/derive_hash_xor_eq.rs:28:10
|
||||
--> $DIR/derive_hash_xor_eq.rs:30:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^
|
||||
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: you are implementing `Hash` explicitly but have derived `PartialEq`
|
||||
--> $DIR/derive_hash_xor_eq.rs:49:5
|
||||
--> $DIR/derive_hash_xor_eq.rs:51:5
|
||||
|
|
||||
LL | / impl Hash for Foo3 {
|
||||
LL | | fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
|
||||
|
|
@ -57,7 +57,7 @@ LL | | }
|
|||
| |_____^
|
||||
|
|
||||
note: `PartialEq` implemented here
|
||||
--> $DIR/derive_hash_xor_eq.rs:46:14
|
||||
--> $DIR/derive_hash_xor_eq.rs:48:14
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^
|
||||
|
|
|
|||
98
tests/ui/derive_partial_eq_without_eq.fixed
Normal file
98
tests/ui/derive_partial_eq_without_eq.fixed
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
// Don't warn on structs that aren't PartialEq
|
||||
struct NotPartialEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq can be derived but is missing
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct MissingEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq is derived
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct NotMissingEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq is manually implemented
|
||||
#[derive(PartialEq)]
|
||||
struct ManualEqImpl {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
impl Eq for ManualEqImpl {}
|
||||
|
||||
// Cannot be Eq because f32 isn't Eq
|
||||
#[derive(PartialEq)]
|
||||
struct CannotBeEq {
|
||||
foo: u32,
|
||||
bar: f32,
|
||||
}
|
||||
|
||||
// Don't warn if PartialEq is manually implemented
|
||||
struct ManualPartialEqImpl {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
impl PartialEq for ManualPartialEqImpl {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.foo == other.foo && self.bar == other.bar
|
||||
}
|
||||
}
|
||||
|
||||
// Generic fields should be properly checked for Eq-ness
|
||||
#[derive(PartialEq)]
|
||||
struct GenericNotEq<T: Eq, U: PartialEq> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct GenericEq<T: Eq, U: Eq> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct TupleStruct(u32);
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct GenericTupleStruct<T: Eq>(T);
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct TupleStructNotEq(f32);
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Enum {
|
||||
Foo(u32),
|
||||
Bar { a: String, b: () },
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum GenericEnum<T: Eq, U: Eq, V: Eq> {
|
||||
Foo(T),
|
||||
Bar { a: U, b: V },
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum EnumNotEq {
|
||||
Foo(u32),
|
||||
Bar { a: String, b: f32 },
|
||||
}
|
||||
|
||||
// Ensure that rustfix works properly when `PartialEq` has other derives on either side
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
struct RustFixWithOtherDerives;
|
||||
|
||||
fn main() {}
|
||||
98
tests/ui/derive_partial_eq_without_eq.rs
Normal file
98
tests/ui/derive_partial_eq_without_eq.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
// Don't warn on structs that aren't PartialEq
|
||||
struct NotPartialEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq can be derived but is missing
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct MissingEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq is derived
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct NotMissingEq {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
// Eq is manually implemented
|
||||
#[derive(PartialEq)]
|
||||
struct ManualEqImpl {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
impl Eq for ManualEqImpl {}
|
||||
|
||||
// Cannot be Eq because f32 isn't Eq
|
||||
#[derive(PartialEq)]
|
||||
struct CannotBeEq {
|
||||
foo: u32,
|
||||
bar: f32,
|
||||
}
|
||||
|
||||
// Don't warn if PartialEq is manually implemented
|
||||
struct ManualPartialEqImpl {
|
||||
foo: u32,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
impl PartialEq for ManualPartialEqImpl {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.foo == other.foo && self.bar == other.bar
|
||||
}
|
||||
}
|
||||
|
||||
// Generic fields should be properly checked for Eq-ness
|
||||
#[derive(PartialEq)]
|
||||
struct GenericNotEq<T: Eq, U: PartialEq> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct GenericEq<T: Eq, U: Eq> {
|
||||
foo: T,
|
||||
bar: U,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct TupleStruct(u32);
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct GenericTupleStruct<T: Eq>(T);
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct TupleStructNotEq(f32);
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Enum {
|
||||
Foo(u32),
|
||||
Bar { a: String, b: () },
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum GenericEnum<T: Eq, U: Eq, V: Eq> {
|
||||
Foo(T),
|
||||
Bar { a: U, b: V },
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum EnumNotEq {
|
||||
Foo(u32),
|
||||
Bar { a: String, b: f32 },
|
||||
}
|
||||
|
||||
// Ensure that rustfix works properly when `PartialEq` has other derives on either side
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
struct RustFixWithOtherDerives;
|
||||
|
||||
fn main() {}
|
||||
46
tests/ui/derive_partial_eq_without_eq.stderr
Normal file
46
tests/ui/derive_partial_eq_without_eq.stderr
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:13:17
|
||||
|
|
||||
LL | #[derive(Debug, PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
|
||||
= note: `-D clippy::derive-partial-eq-without-eq` implied by `-D warnings`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:61:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:67:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:70:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:76:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:82:10
|
||||
|
|
||||
LL | #[derive(PartialEq)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: you are deriving `PartialEq` and can implement `Eq`
|
||||
--> $DIR/derive_partial_eq_without_eq.rs:95:17
|
||||
|
|
||||
LL | #[derive(Debug, PartialEq, Clone)]
|
||||
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused_variables, dead_code)]
|
||||
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
|
||||
#![warn(clippy::equatable_if_let)]
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused_variables, dead_code)]
|
||||
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
|
||||
#![warn(clippy::equatable_if_let)]
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
#![warn(clippy::unit_cmp)]
|
||||
#![allow(clippy::no_effect, clippy::unnecessary_operation)]
|
||||
#![allow(
|
||||
clippy::no_effect,
|
||||
clippy::unnecessary_operation,
|
||||
clippy::derive_partial_eq_without_eq
|
||||
)]
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct ContainsUnit(()); // should be fine
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: ==-comparison of unit values detected. This will always be true
|
||||
--> $DIR/unit_cmp.rs:12:8
|
||||
--> $DIR/unit_cmp.rs:16:8
|
||||
|
|
||||
LL | if {
|
||||
| ________^
|
||||
|
|
@ -12,7 +12,7 @@ LL | | } {}
|
|||
= note: `-D clippy::unit-cmp` implied by `-D warnings`
|
||||
|
||||
error: >-comparison of unit values detected. This will always be false
|
||||
--> $DIR/unit_cmp.rs:18:8
|
||||
--> $DIR/unit_cmp.rs:22:8
|
||||
|
|
||||
LL | if {
|
||||
| ________^
|
||||
|
|
@ -23,7 +23,7 @@ LL | | } {}
|
|||
| |_____^
|
||||
|
||||
error: `assert_eq` of unit values detected. This will always succeed
|
||||
--> $DIR/unit_cmp.rs:24:5
|
||||
--> $DIR/unit_cmp.rs:28:5
|
||||
|
|
||||
LL | / assert_eq!(
|
||||
LL | | {
|
||||
|
|
@ -35,7 +35,7 @@ LL | | );
|
|||
| |_____^
|
||||
|
||||
error: `debug_assert_eq` of unit values detected. This will always succeed
|
||||
--> $DIR/unit_cmp.rs:32:5
|
||||
--> $DIR/unit_cmp.rs:36:5
|
||||
|
|
||||
LL | / debug_assert_eq!(
|
||||
LL | | {
|
||||
|
|
@ -47,7 +47,7 @@ LL | | );
|
|||
| |_____^
|
||||
|
||||
error: `assert_ne` of unit values detected. This will always fail
|
||||
--> $DIR/unit_cmp.rs:41:5
|
||||
--> $DIR/unit_cmp.rs:45:5
|
||||
|
|
||||
LL | / assert_ne!(
|
||||
LL | | {
|
||||
|
|
@ -59,7 +59,7 @@ LL | | );
|
|||
| |_____^
|
||||
|
||||
error: `debug_assert_ne` of unit values detected. This will always fail
|
||||
--> $DIR/unit_cmp.rs:49:5
|
||||
--> $DIR/unit_cmp.rs:53:5
|
||||
|
|
||||
LL | / debug_assert_ne!(
|
||||
LL | | {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue