rust/tests/ui/ptr_eq_no_std.fixed
Samuel Tardieu cbfe1f5f58 Restrict the cases where ptr_eq triggers
`ptr_eq` was recently enhanced to lint on more cases of raw pointers
comparison:

- lint on all raw pointer comparison, by proposing to use
  `[core|std]::ptr::eq(lhs, rhs)` instead of `lhs == rhs`;
- removing one symetric `as usize` on each size if needed
- peeling any level of `as *[const|mut] _` if the remaining expression
  can still be coerced into the original one (i.e., is a ref or raw
  pointer to the same type as before)

The current change restricts the lint to the cases where at least one
level of symetric `as usize`, or any conversion to a raw pointer, could
be removed. For example, a direct comparaison of two raw pointers will
not trigger the lint anymore.
2025-04-08 18:16:41 +02:00

58 lines
1.2 KiB
Rust

#![warn(clippy::ptr_eq)]
#![no_std]
#![feature(lang_items)]
use core::panic::PanicInfo;
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
loop {}
}
macro_rules! mac {
($a:expr, $b:expr) => {
$a as *const _ as usize == $b as *const _ as usize
};
}
macro_rules! another_mac {
($a:expr, $b:expr) => {
$a as *const _ == $b as *const _
};
}
fn main() {
let a = &[1, 2, 3];
let b = &[1, 2, 3];
let _ = core::ptr::eq(a, b);
//~^ ptr_eq
let _ = core::ptr::eq(a, b);
//~^ ptr_eq
// Do not lint: the rhs conversion is needed
let _ = a.as_ptr() == b as *const _;
// Do not lint: we have two raw pointers already
let _ = a.as_ptr() == b.as_ptr();
// Do not lint
let _ = mac!(a, b);
let _ = another_mac!(a, b);
let a = &mut [1, 2, 3];
let b = &mut [1, 2, 3];
// Do not lint: the rhs conversion is needed
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _;
// Do not lint: we have two raw pointers already
let _ = a.as_mut_ptr() == b.as_mut_ptr();
let _ = a == b;
let _ = core::ptr::eq(a, b);
}