@@ -21,6 +21,7 @@ mod modulo_one;
21
21
mod needless_bitwise_bool;
22
22
mod numeric_arithmetic;
23
23
mod op_ref;
24
+ mod ptr_eq;
24
25
mod verbose_bit_mask;
25
26
26
27
declare_clippy_lint ! {
@@ -671,6 +672,35 @@ declare_clippy_lint! {
671
672
"Boolean expressions that use bitwise rather than lazy operators"
672
673
}
673
674
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
+
674
704
pub struct Operators {
675
705
arithmetic_context : numeric_arithmetic:: Context ,
676
706
verbose_bit_mask_threshold : u64 ,
@@ -699,6 +729,7 @@ impl_lint_pass!(Operators => [
699
729
MODULO_ONE ,
700
730
MODULO_ARITHMETIC ,
701
731
NEEDLESS_BITWISE_BOOL ,
732
+ PTR_EQ ,
702
733
] ) ;
703
734
impl Operators {
704
735
pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -722,6 +753,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
722
753
erasing_op:: check ( cx, e, op. node , lhs, rhs) ;
723
754
identity_op:: check ( cx, e, op. node , lhs, rhs) ;
724
755
needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
756
+ ptr_eq:: check ( cx, e, op. node , lhs, rhs) ;
725
757
}
726
758
self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
727
759
bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments