Closed
Description
Code
#![allow(dead_code)]
use std::ptr::NonNull;
trait Foo {}
fn test(a: NonNull<dyn Foo>, b: NonNull<dyn Foo>) -> bool {
a == b
}
fn main() {}
Current output
<no warnings>
Desired output
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> src/main.rs:8:5
|
8 | a == b
| ^^^^^^
|
= note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
|
8 | std::ptr::addr_eq(a.as_ptr(), b.as_ptr())
| ++++++++++++++++++ ++++++++++ ++++++++++
Rationale and extra context
#117758 added the ambiguous_wide_pointer_comparisons
lint, but the implementation doesn't consider pointer types beyond *const T
and *mut T
.
Other cases
I've also found that explicit PartialOrd::partial_cmp
and Ord::cmp
calls are not warned with ordinary pointers.
fn test_1(a: *const dyn Foo, b: *const dyn Foo) -> Option<std::cmp::Ordering> {
a.partial_cmp(&b) // No warning.
}
fn test_2(a: *const dyn Foo, b: *const dyn Foo) -> std::cmp::Ordering {
a.cmp(&b) // No warning.
}
Rust Version
rustc 1.78.0-nightly (6672c16af 2024-02-17)
binary: rustc
commit-hash: 6672c16afcd4db8acdf08a6984fd4107bf07632c
commit-date: 2024-02-17
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Anything else?
No response