Closed
Description
Code
use std::cmp::Ordering;
fn ptr_cmp<T: ?Sized>(p1: *const T, p2: *const T) -> Ordering {
p1.cmp(&p2)
}
fn less_than<T: ?Sized>(p1: *const T, p2: *const T) -> bool {
p1 < p2
}
fn main() {
let s = "abc";
assert_eq!(ptr_cmp(s, s), Ordering::Equal);
assert_eq!(ptr_cmp(&s[..1], s), Ordering::Less);
assert_eq!(less_than(s, s), false);
assert_eq!(less_than(&s[..1], s), true);
}
Current output
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> src/main.rs:4:5
|
4 | p1.cmp(&p2)
| ^^^^^^^^^^^
|
= note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
|
4 | p1.cast::<()>().cmp(&p2.cast::<()>())
| +++++++++++++ +++++++++++++
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> src/main.rs:8:5
|
8 | p1 < p2
| ^^^^^^^
|
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
|
8 | p1.cast::<()>() < p2.cast::<()>()
| +++++++++++++ +++++++++++++
warning: `wide-ptr-cmp` (bin "wide-ptr-cmp") generated 2 warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Desired output
No output
Rationale and extra context
The warning references std::ptr::addr_eq
, however that can't be used for ordering two pointers.
Either std::ptr::cmp
should be added to std
(and core
) which can be referenced instead or the lint should simply not be triggered by <pointer>::cmp
and <pointer>::partial_cmp
.
Other cases
Rust Version
$ rustc --version --verbose
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1
Anything else?
No response