@@ -21,6 +21,7 @@ mod modulo_one;
2121mod needless_bitwise_bool;
2222mod numeric_arithmetic;
2323mod op_ref;
24+ mod ptr_eq;
2425mod verbose_bit_mask;
2526
2627declare_clippy_lint ! {
@@ -670,6 +671,35 @@ declare_clippy_lint! {
670671 "Boolean expressions that use bitwise rather than lazy operators"
671672}
672673
674+ declare_clippy_lint ! {
675+ /// ### What it does
676+ /// Use `std::ptr::eq` when applicable
677+ ///
678+ /// ### Why is this bad?
679+ /// `ptr::eq` can be used to compare `&T` references
680+ /// (which coerce to `*const T` implicitly) by their address rather than
681+ /// comparing the values they point to.
682+ ///
683+ /// ### Example
684+ /// ```rust
685+ /// let a = &[1, 2, 3];
686+ /// let b = &[1, 2, 3];
687+ ///
688+ /// assert!(a as *const _ as usize == b as *const _ as usize);
689+ /// ```
690+ /// Use instead:
691+ /// ```rust
692+ /// let a = &[1, 2, 3];
693+ /// let b = &[1, 2, 3];
694+ ///
695+ /// assert!(std::ptr::eq(a, b));
696+ /// ```
697+ #[ clippy:: version = "1.49.0" ]
698+ pub PTR_EQ ,
699+ style,
700+ "use `std::ptr::eq` when comparing raw pointers"
701+ }
702+
673703pub struct Operators {
674704 arithmetic_context : numeric_arithmetic:: Context ,
675705 verbose_bit_mask_threshold : u64 ,
@@ -698,6 +728,7 @@ impl_lint_pass!(Operators => [
698728 MODULO_ONE ,
699729 MODULO_ARITHMETIC ,
700730 NEEDLESS_BITWISE_BOOL ,
731+ PTR_EQ ,
701732] ) ;
702733impl Operators {
703734 pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -721,6 +752,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
721752 erasing_op:: check ( cx, e, op. node , lhs, rhs) ;
722753 identity_op:: check ( cx, e, op. node , lhs, rhs) ;
723754 needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
755+ ptr_eq:: check ( cx, e, op. node , lhs, rhs) ;
724756 }
725757 self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
726758 bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments