about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/derive_partial_eq_without_eq.rs
blob: 073330468d3189f7703d05e831e76dfd8073c37e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#![allow(unused)]
#![warn(clippy::derive_partial_eq_without_eq)]

// Don't warn on structs that aren't PartialEq
pub struct NotPartialEq {
    foo: u32,
    bar: String,
}

// Eq can be derived but is missing
#[derive(Debug, PartialEq)]
//~^ derive_partial_eq_without_eq
pub struct MissingEq {
    foo: u32,
    bar: String,
}

// Check that we honor the `allow` attribute
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct AllowedMissingEq {
    foo: u32,
    bar: String,
}

// Check that we honor the `expect` attribute
#[expect(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct ExpectedMissingEq {
    foo: u32,
    bar: String,
}

// Eq is derived
#[derive(PartialEq, Eq)]
pub struct NotMissingEq {
    foo: u32,
    bar: String,
}

// Eq is manually implemented
#[derive(PartialEq)]
pub struct ManualEqImpl {
    foo: u32,
    bar: String,
}

impl Eq for ManualEqImpl {}

// Cannot be Eq because f32 isn't Eq
#[derive(PartialEq)]
pub struct CannotBeEq {
    foo: u32,
    bar: f32,
}

// Don't warn if PartialEq is manually implemented
pub 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)]
//~^ derive_partial_eq_without_eq
pub struct GenericNotEq<T: Eq, U: PartialEq> {
    foo: T,
    bar: U,
}

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub struct GenericEq<T: Eq, U: Eq> {
    foo: T,
    bar: U,
}

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub struct TupleStruct(u32);

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub struct GenericTupleStruct<T: Eq>(T);

#[derive(PartialEq)]
pub struct TupleStructNotEq(f32);

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub enum Enum {
    Foo(u32),
    Bar { a: String, b: () },
}

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub enum GenericEnum<T: Eq, U: Eq, V: Eq> {
    Foo(T),
    Bar { a: U, b: V },
}

#[derive(PartialEq)]
pub 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)]
//~^ derive_partial_eq_without_eq
pub struct RustFixWithOtherDerives;

#[derive(PartialEq)]
//~^ derive_partial_eq_without_eq
pub struct Generic<T>(T);

#[derive(PartialEq, Eq)]
pub struct GenericPhantom<T>(core::marker::PhantomData<T>);

mod _hidden {
    #[derive(PartialEq)]
    //~^ derive_partial_eq_without_eq
    pub struct Reexported;

    #[derive(PartialEq)]
    //~^ derive_partial_eq_without_eq
    pub struct InPubFn;

    #[derive(PartialEq)]
    pub(crate) struct PubCrate;

    #[derive(PartialEq)]
    pub(super) struct PubSuper;
}

pub use _hidden::Reexported;
pub fn _from_mod() -> _hidden::InPubFn {
    _hidden::InPubFn
}

#[derive(PartialEq)]
struct InternalTy;

// This is a `non_exhaustive` type so should not warn.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub struct MissingEqNonExhaustive {
    foo: u32,
    bar: String,
}

// This is a `non_exhaustive` type so should not warn.
#[derive(Debug, PartialEq)]
pub struct MissingEqNonExhaustive1 {
    foo: u32,
    #[non_exhaustive]
    bar: String,
}

// This is a `non_exhaustive` type so should not warn.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum MissingEqNonExhaustive2 {
    Foo,
    Bar,
}

// This is a `non_exhaustive` type so should not warn.
#[derive(Debug, PartialEq)]
pub enum MissingEqNonExhaustive3 {
    Foo,
    #[non_exhaustive]
    Bar,
}

mod struct_gen {
    // issue 9413
    pub trait Group {
        type Element: Eq + PartialEq;
    }

    pub trait Suite {
        type Group: Group;
    }

    #[derive(PartialEq)]
    //~^ derive_partial_eq_without_eq

    pub struct Foo<C: Suite>(<C::Group as Group>::Element);

    #[derive(PartialEq, Eq)]
    pub struct Bar<C: Suite>(i32, <C::Group as Group>::Element);

    // issue 9319
    #[derive(PartialEq)]
    //~^ derive_partial_eq_without_eq

    pub struct Oof<T: Fn()>(T);

    #[derive(PartialEq, Eq)]
    pub struct Rab<T: Fn()>(T);
}

fn main() {}