@@ -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 ! {
@@ -671,6 +672,35 @@ declare_clippy_lint! {
671672 "Boolean expressions that use bitwise rather than lazy operators"
672673}
673674
675+ declare_clippy_lint ! {
676+ /// ### What it does
677+ /// Use `std::ptr::eq` when applicable
678+ ///
679+ /// ### Why is this bad?
680+ /// `ptr::eq` can be used to compare `&T` references
681+ /// (which coerce to `*const T` implicitly) by their address rather than
682+ /// comparing the values they point to.
683+ ///
684+ /// ### Example
685+ /// ```rust
686+ /// let a = &[1, 2, 3];
687+ /// let b = &[1, 2, 3];
688+ ///
689+ /// assert!(a as *const _ as usize == b as *const _ as usize);
690+ /// ```
691+ /// Use instead:
692+ /// ```rust
693+ /// let a = &[1, 2, 3];
694+ /// let b = &[1, 2, 3];
695+ ///
696+ /// assert!(std::ptr::eq(a, b));
697+ /// ```
698+ #[ clippy:: version = "1.49.0" ]
699+ pub PTR_EQ ,
700+ style,
701+ "use `std::ptr::eq` when comparing raw pointers"
702+ }
703+
674704pub struct Operators {
675705 arithmetic_context : numeric_arithmetic:: Context ,
676706 verbose_bit_mask_threshold : u64 ,
@@ -699,6 +729,7 @@ impl_lint_pass!(Operators => [
699729 MODULO_ONE ,
700730 MODULO_ARITHMETIC ,
701731 NEEDLESS_BITWISE_BOOL ,
732+ PTR_EQ ,
702733] ) ;
703734impl Operators {
704735 pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -722,6 +753,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
722753 erasing_op:: check ( cx, e, op. node , lhs, rhs) ;
723754 identity_op:: check ( cx, e, op. node , lhs, rhs) ;
724755 needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
756+ ptr_eq:: check ( cx, e, op. node , lhs, rhs) ;
725757 }
726758 self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
727759 bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments