Skip to content

Commit eff5776

Browse files
committed
Address clippy::incorrect_partial_ord_impl_on_ord_type
1 parent ec25e61 commit eff5776

File tree

7 files changed

+236
-102
lines changed

7 files changed

+236
-102
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Changed
11+
- `PartialOrd` implementations now use `Ord` if applicable.
12+
813
## [1.2.1] - 2022-04-14
914

1015
### Fixed

src/test/basic.rs

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ fn struct_() -> Result<()> {
8383
impl<T> ::core::cmp::PartialOrd for Test<T> {
8484
#[inline]
8585
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
86-
match (self, __other) {
87-
(Test { field: ref __field }, Test { field: ref __other_field }) =>
88-
match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) {
89-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
90-
__cmp => __cmp,
91-
},
92-
}
86+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, __other))
9387
}
9488
}
9589
},
@@ -176,13 +170,7 @@ fn tuple() -> Result<()> {
176170
impl<T> ::core::cmp::PartialOrd for Test<T> {
177171
#[inline]
178172
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
179-
match (self, __other) {
180-
(Test(ref __0), Test(ref __other_0)) =>
181-
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
182-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
183-
__cmp => __cmp,
184-
},
185-
}
173+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, __other))
186174
}
187175
}
188176
},
@@ -235,39 +223,6 @@ fn enum_() -> Result<()> {
235223
Test::E => ::core::cmp::Ordering::Greater,
236224
}
237225
};
238-
#[cfg(feature = "nightly")]
239-
let partial_ord = quote! {
240-
::core::cmp::PartialOrd::partial_cmp(&__self_disc, &__other_disc)
241-
};
242-
#[cfg(not(any(feature = "nightly", feature = "safe")))]
243-
let partial_ord = quote! {
244-
::core::cmp::PartialOrd::partial_cmp(
245-
&unsafe { ::core::mem::transmute::<_, isize>(__self_disc) },
246-
&unsafe { ::core::mem::transmute::<_, isize>(__other_disc) },
247-
)
248-
};
249-
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
250-
let partial_ord = quote! {
251-
match self {
252-
Test::A { field: ref __field } => ::core::option::Option::Some(::core::cmp::Ordering::Less),
253-
Test::B { } =>
254-
match __other {
255-
Test::A { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
256-
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
257-
},
258-
Test::C(ref __0) =>
259-
match __other {
260-
Test::A { .. } | Test::B { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
261-
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
262-
},
263-
Test::D() =>
264-
match __other {
265-
Test::A { .. } | Test::B { .. } | Test::C(..) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
266-
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
267-
},
268-
Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
269-
}
270-
};
271226

272227
test_derive(
273228
quote! {
@@ -404,25 +359,7 @@ fn enum_() -> Result<()> {
404359
impl<T> ::core::cmp::PartialOrd for Test<T> {
405360
#[inline]
406361
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
407-
#discriminant
408-
409-
if __self_disc == __other_disc {
410-
match (self, __other) {
411-
(Test::A { field: ref __field }, Test::A { field: ref __other_field }) =>
412-
match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) {
413-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
414-
__cmp => __cmp,
415-
},
416-
(Test::C(ref __0), Test::C(ref __other_0)) =>
417-
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
418-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
419-
__cmp => __cmp,
420-
},
421-
_ => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
422-
}
423-
} else {
424-
#partial_ord
425-
}
362+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, __other))
426363
}
427364
}
428365
},

src/test/bound.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,7 @@ fn check_trait_bounds() -> Result<()> {
246246
{
247247
#[inline]
248248
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
249-
match (self, __other) {
250-
(Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) =>
251-
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
252-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => match ::core::cmp::PartialOrd::partial_cmp(__1, __other_1) {
253-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
254-
__cmp => __cmp,
255-
},
256-
__cmp => __cmp,
257-
},
258-
}
249+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, __other))
259250
}
260251
}
261252
},
@@ -387,16 +378,7 @@ fn check_multiple_trait_bounds() -> Result<()> {
387378
{
388379
#[inline]
389380
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
390-
match (self, __other) {
391-
(Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) =>
392-
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
393-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => match ::core::cmp::PartialOrd::partial_cmp(__1, __other_1) {
394-
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
395-
__cmp => __cmp,
396-
},
397-
__cmp => __cmp,
398-
},
399-
}
381+
::core::option::Option::Some(::core::cmp::Ord::cmp(self, __other))
400382
}
401383
}
402384
},

src/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod enum_;
44
#[cfg(not(any(feature = "nightly", feature = "safe")))]
55
mod incomparable;
66
mod misc;
7+
mod partial_ord;
78
mod skip;
89
mod use_case;
910
#[cfg(feature = "zeroize")]

src/test/partial_ord.rs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
use quote::quote;
2+
use syn::Result;
3+
4+
use super::test_derive;
5+
6+
#[test]
7+
fn struct_() -> Result<()> {
8+
test_derive(
9+
quote! {
10+
#[derive_where(PartialOrd)]
11+
struct Test<T> { field: std::marker::PhatomData<T> }
12+
},
13+
quote! {
14+
impl<T> ::core::cmp::PartialOrd for Test<T> {
15+
#[inline]
16+
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
17+
match (self, __other) {
18+
(Test { field: ref __field }, Test { field: ref __other_field }) =>
19+
match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) {
20+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
21+
__cmp => __cmp,
22+
},
23+
}
24+
}
25+
}
26+
},
27+
)
28+
}
29+
30+
#[test]
31+
fn tuple() -> Result<()> {
32+
test_derive(
33+
quote! {
34+
#[derive_where(PartialOrd)]
35+
struct Test<T>(std::marker::PhatomData<T>);
36+
},
37+
quote! {
38+
impl<T> ::core::cmp::PartialOrd for Test<T> {
39+
#[inline]
40+
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
41+
match (self, __other) {
42+
(Test(ref __0), Test(ref __other_0)) =>
43+
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
44+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
45+
__cmp => __cmp,
46+
},
47+
}
48+
}
49+
}
50+
},
51+
)
52+
}
53+
54+
#[test]
55+
fn enum_() -> Result<()> {
56+
#[cfg(feature = "nightly")]
57+
let discriminant = quote! {
58+
let __self_disc = ::core::intrinsics::discriminant_value(self);
59+
let __other_disc = ::core::intrinsics::discriminant_value(__other);
60+
};
61+
#[cfg(not(feature = "nightly"))]
62+
let discriminant = quote! {
63+
let __self_disc = ::core::mem::discriminant(self);
64+
let __other_disc = ::core::mem::discriminant(__other);
65+
};
66+
#[cfg(feature = "nightly")]
67+
let partial_ord = quote! {
68+
::core::cmp::PartialOrd::partial_cmp(&__self_disc, &__other_disc)
69+
};
70+
#[cfg(not(any(feature = "nightly", feature = "safe")))]
71+
let partial_ord = quote! {
72+
::core::cmp::PartialOrd::partial_cmp(
73+
&unsafe { ::core::mem::transmute::<_, isize>(__self_disc) },
74+
&unsafe { ::core::mem::transmute::<_, isize>(__other_disc) },
75+
)
76+
};
77+
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
78+
let partial_ord = quote! {
79+
match self {
80+
Test::A { field: ref __field } => ::core::option::Option::Some(::core::cmp::Ordering::Less),
81+
Test::B { } =>
82+
match __other {
83+
Test::A { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
84+
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
85+
},
86+
Test::C(ref __0) =>
87+
match __other {
88+
Test::A { .. } | Test::B { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
89+
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
90+
},
91+
Test::D() =>
92+
match __other {
93+
Test::A { .. } | Test::B { .. } | Test::C(..) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
94+
_ => ::core::option::Option::Some(::core::cmp::Ordering::Less),
95+
},
96+
Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
97+
}
98+
};
99+
100+
test_derive(
101+
quote! {
102+
#[derive_where(PartialOrd)]
103+
enum Test<T> {
104+
A { field: std::marker::PhatomData<T>},
105+
B { },
106+
C(std::marker::PhatomData<T>),
107+
D(),
108+
E,
109+
}
110+
},
111+
quote! {
112+
impl<T> ::core::cmp::PartialOrd for Test<T> {
113+
#[inline]
114+
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
115+
#discriminant
116+
117+
if __self_disc == __other_disc {
118+
match (self, __other) {
119+
(Test::A { field: ref __field }, Test::A { field: ref __other_field }) =>
120+
match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) {
121+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
122+
__cmp => __cmp,
123+
},
124+
(Test::C(ref __0), Test::C(ref __other_0)) =>
125+
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
126+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
127+
__cmp => __cmp,
128+
},
129+
_ => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
130+
}
131+
} else {
132+
#partial_ord
133+
}
134+
}
135+
}
136+
},
137+
)
138+
}
139+
140+
#[test]
141+
fn union_() -> Result<()> {
142+
test_derive(
143+
quote! {
144+
#[derive_where(Clone, Copy)]
145+
union Test<T> {
146+
a: std::marker::PhantomData<T>,
147+
b: u8,
148+
}
149+
},
150+
quote! {
151+
impl<T> ::core::clone::Clone for Test<T> {
152+
#[inline]
153+
fn clone(&self) -> Self {
154+
*self
155+
}
156+
}
157+
158+
impl<T> ::core::marker::Copy for Test<T>
159+
{ }
160+
},
161+
)
162+
}
163+
164+
#[test]
165+
fn bound() -> Result<()> {
166+
test_derive(
167+
quote! {
168+
#[derive_where(Ord; T)]
169+
#[derive_where(PartialOrd)]
170+
struct Test<T, U>(T, std::marker::PhantomData<U>);
171+
},
172+
quote! {
173+
impl<T, U> ::core::cmp::Ord for Test<T, U>
174+
where T: ::core::cmp::Ord
175+
{
176+
#[inline]
177+
fn cmp(&self, __other: &Self) -> ::core::cmp::Ordering {
178+
match (self, __other) {
179+
(Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) =>
180+
match ::core::cmp::Ord::cmp(__0, __other_0) {
181+
::core::cmp::Ordering::Equal => match ::core::cmp::Ord::cmp(__1, __other_1) {
182+
::core::cmp::Ordering::Equal => ::core::cmp::Ordering::Equal,
183+
__cmp => __cmp,
184+
},
185+
__cmp => __cmp,
186+
},
187+
}
188+
}
189+
}
190+
191+
impl<T, U> ::core::cmp::PartialOrd for Test<T, U> {
192+
#[inline]
193+
fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
194+
match (self, __other) {
195+
(Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) =>
196+
match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) {
197+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => match ::core::cmp::PartialOrd::partial_cmp(__1, __other_1) {
198+
::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal),
199+
__cmp => __cmp,
200+
},
201+
__cmp => __cmp,
202+
},
203+
}
204+
}
205+
}
206+
},
207+
)
208+
}

0 commit comments

Comments
 (0)