@@ -21,6 +21,7 @@ mod modulo_arithmetic;
2121mod modulo_one;
2222mod needless_bitwise_bool;
2323mod op_ref;
24+ mod ptr_eq;
2425mod verbose_bit_mask;
2526
2627declare_clippy_lint ! {
@@ -656,6 +657,35 @@ declare_clippy_lint! {
656657 "Boolean expressions that use bitwise rather than lazy operators"
657658}
658659
660+ declare_clippy_lint ! {
661+ /// ### What it does
662+ /// Use `std::ptr::eq` when applicable
663+ ///
664+ /// ### Why is this bad?
665+ /// `ptr::eq` can be used to compare `&T` references
666+ /// (which coerce to `*const T` implicitly) by their address rather than
667+ /// comparing the values they point to.
668+ ///
669+ /// ### Example
670+ /// ```rust
671+ /// let a = &[1, 2, 3];
672+ /// let b = &[1, 2, 3];
673+ ///
674+ /// assert!(a as *const _ as usize == b as *const _ as usize);
675+ /// ```
676+ /// Use instead:
677+ /// ```rust
678+ /// let a = &[1, 2, 3];
679+ /// let b = &[1, 2, 3];
680+ ///
681+ /// assert!(std::ptr::eq(a, b));
682+ /// ```
683+ #[ clippy:: version = "1.49.0" ]
684+ pub PTR_EQ ,
685+ style,
686+ "use `std::ptr::eq` when comparing raw pointers"
687+ }
688+
659689pub struct Operators {
660690 arithmetic_context : arithmetic:: Context ,
661691 verbose_bit_mask_threshold : u64 ,
@@ -684,6 +714,7 @@ impl_lint_pass!(Operators => [
684714 MODULO_ONE ,
685715 MODULO_ARITHMETIC ,
686716 NEEDLESS_BITWISE_BOOL ,
717+ PTR_EQ ,
687718] ) ;
688719impl Operators {
689720 pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -707,6 +738,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
707738 erasing_op:: check ( cx, e, op. node , lhs, rhs) ;
708739 identity_op:: check ( cx, e, op. node , lhs, rhs) ;
709740 needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
741+ ptr_eq:: check ( cx, e, op. node , lhs, rhs) ;
710742 }
711743 self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
712744 bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments