Skip to content

Commit f732282

Browse files
committed
Auto merge of rust-lang#121268 - Urgau:improve_ambi_wide_ptr_cmps, r=Nadrieril
Add detection of [Partial]Ord methods in the `ambiguous_wide_pointer_comparisons` lint Partially addresses rust-lang#121264 by adding diagnostics items for PartialOrd and Ord methods, detecting such diagnostics items as "binary operation" and suggesting the correct replacement. I also took the opportunity to change the suggestion to use new methods `.cast()` on `*mut T` an d `*const T`.
2 parents c7dbd27 + 1721d80 commit f732282

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

core/src/cmp.rs

+7
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
848848
#[stable(feature = "ord_max_min", since = "1.21.0")]
849849
#[inline]
850850
#[must_use]
851+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_ord_max")]
851852
fn max(self, other: Self) -> Self
852853
where
853854
Self: Sized,
@@ -868,6 +869,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
868869
#[stable(feature = "ord_max_min", since = "1.21.0")]
869870
#[inline]
870871
#[must_use]
872+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_ord_min")]
871873
fn min(self, other: Self) -> Self
872874
where
873875
Self: Sized,
@@ -1154,6 +1156,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
11541156
/// ```
11551157
#[must_use]
11561158
#[stable(feature = "rust1", since = "1.0.0")]
1159+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialord_cmp")]
11571160
fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
11581161

11591162
/// This method tests less than (for `self` and `other`) and is used by the `<` operator.
@@ -1168,6 +1171,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
11681171
#[inline]
11691172
#[must_use]
11701173
#[stable(feature = "rust1", since = "1.0.0")]
1174+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialord_lt")]
11711175
fn lt(&self, other: &Rhs) -> bool {
11721176
matches!(self.partial_cmp(other), Some(Less))
11731177
}
@@ -1185,6 +1189,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
11851189
#[inline]
11861190
#[must_use]
11871191
#[stable(feature = "rust1", since = "1.0.0")]
1192+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialord_le")]
11881193
fn le(&self, other: &Rhs) -> bool {
11891194
matches!(self.partial_cmp(other), Some(Less | Equal))
11901195
}
@@ -1201,6 +1206,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
12011206
#[inline]
12021207
#[must_use]
12031208
#[stable(feature = "rust1", since = "1.0.0")]
1209+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialord_gt")]
12041210
fn gt(&self, other: &Rhs) -> bool {
12051211
matches!(self.partial_cmp(other), Some(Greater))
12061212
}
@@ -1218,6 +1224,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
12181224
#[inline]
12191225
#[must_use]
12201226
#[stable(feature = "rust1", since = "1.0.0")]
1227+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialord_ge")]
12211228
fn ge(&self, other: &Rhs) -> bool {
12221229
matches!(self.partial_cmp(other), Some(Greater | Equal))
12231230
}

core/src/ptr/const_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ impl<T: ?Sized> Ord for *const T {
18571857
#[stable(feature = "rust1", since = "1.0.0")]
18581858
impl<T: ?Sized> PartialOrd for *const T {
18591859
#[inline]
1860+
#[allow(ambiguous_wide_pointer_comparisons)]
18601861
fn partial_cmp(&self, other: &*const T) -> Option<Ordering> {
18611862
Some(self.cmp(other))
18621863
}

core/src/ptr/metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl<Dyn: ?Sized> PartialEq for DynMetadata<Dyn> {
258258

259259
impl<Dyn: ?Sized> Ord for DynMetadata<Dyn> {
260260
#[inline]
261+
#[allow(ambiguous_wide_pointer_comparisons)]
261262
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
262263
(self.vtable_ptr as *const VTable).cmp(&(other.vtable_ptr as *const VTable))
263264
}

core/src/ptr/mut_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,7 @@ impl<T: ?Sized> Ord for *mut T {
22752275
#[stable(feature = "rust1", since = "1.0.0")]
22762276
impl<T: ?Sized> PartialOrd for *mut T {
22772277
#[inline(always)]
2278+
#[allow(ambiguous_wide_pointer_comparisons)]
22782279
fn partial_cmp(&self, other: &*mut T) -> Option<Ordering> {
22792280
Some(self.cmp(other))
22802281
}

core/src/ptr/non_null.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,7 @@ impl<T: ?Sized> PartialEq for NonNull<T> {
18211821
#[stable(feature = "nonnull", since = "1.25.0")]
18221822
impl<T: ?Sized> Ord for NonNull<T> {
18231823
#[inline]
1824+
#[allow(ambiguous_wide_pointer_comparisons)]
18241825
fn cmp(&self, other: &Self) -> Ordering {
18251826
self.as_ptr().cmp(&other.as_ptr())
18261827
}
@@ -1829,6 +1830,7 @@ impl<T: ?Sized> Ord for NonNull<T> {
18291830
#[stable(feature = "nonnull", since = "1.25.0")]
18301831
impl<T: ?Sized> PartialOrd for NonNull<T> {
18311832
#[inline]
1833+
#[allow(ambiguous_wide_pointer_comparisons)]
18321834
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
18331835
self.as_ptr().partial_cmp(&other.as_ptr())
18341836
}

0 commit comments

Comments
 (0)