@@ -786,9 +786,9 @@ fn check_ptr_eq<'tcx>(
786
786
}
787
787
788
788
// Remove one level of usize conversion if any
789
- let ( left, right) = match ( expr_as_cast_to_usize ( cx, left) , expr_as_cast_to_usize ( cx, right) ) {
790
- ( Some ( lhs) , Some ( rhs) ) => ( lhs, rhs) ,
791
- _ => ( left, right) ,
789
+ let ( left, right, usize_peeled ) = match ( expr_as_cast_to_usize ( cx, left) , expr_as_cast_to_usize ( cx, right) ) {
790
+ ( Some ( lhs) , Some ( rhs) ) => ( lhs, rhs, true ) ,
791
+ _ => ( left, right, false ) ,
792
792
} ;
793
793
794
794
// This lint concerns raw pointers
@@ -797,10 +797,16 @@ fn check_ptr_eq<'tcx>(
797
797
return ;
798
798
}
799
799
800
- let ( left_var, right_var) = ( peel_raw_casts ( cx, left, left_ty) , peel_raw_casts ( cx, right, right_ty) ) ;
800
+ let ( ( left_var, left_casts_peeled) , ( right_var, right_casts_peeled) ) =
801
+ ( peel_raw_casts ( cx, left, left_ty) , peel_raw_casts ( cx, right, right_ty) ) ;
801
802
802
- if let Some ( left_snip) = left_var. span . get_source_text ( cx)
803
- && let Some ( right_snip) = right_var. span . get_source_text ( cx)
803
+ if !( usize_peeled || left_casts_peeled || right_casts_peeled) {
804
+ return ;
805
+ }
806
+
807
+ let mut app = Applicability :: MachineApplicable ;
808
+ let left_snip = Sugg :: hir_with_context ( cx, left_var, expr. span . ctxt ( ) , "_" , & mut app) ;
809
+ let right_snip = Sugg :: hir_with_context ( cx, right_var, expr. span . ctxt ( ) , "_" , & mut app) ;
804
810
{
805
811
let Some ( top_crate) = std_or_core ( cx) else { return } ;
806
812
let invert = if op == BinOpKind :: Eq { "" } else { "!" } ;
@@ -811,15 +817,16 @@ fn check_ptr_eq<'tcx>(
811
817
format ! ( "use `{top_crate}::ptr::eq` when comparing raw pointers" ) ,
812
818
"try" ,
813
819
format ! ( "{invert}{top_crate}::ptr::eq({left_snip}, {right_snip})" ) ,
814
- Applicability :: MachineApplicable ,
820
+ app ,
815
821
) ;
816
822
}
817
823
}
818
824
819
825
// If the given expression is a cast to a usize, return the lhs of the cast
820
826
// E.g., `foo as *const _ as usize` returns `foo as *const _`.
821
827
fn expr_as_cast_to_usize < ' tcx > ( cx : & LateContext < ' tcx > , cast_expr : & ' tcx Expr < ' _ > ) -> Option < & ' tcx Expr < ' tcx > > {
822
- if cx. typeck_results ( ) . expr_ty ( cast_expr) == cx. tcx . types . usize
828
+ if !cast_expr. span . from_expansion ( )
829
+ && cx. typeck_results ( ) . expr_ty ( cast_expr) == cx. tcx . types . usize
823
830
&& let ExprKind :: Cast ( expr, _) = cast_expr. kind
824
831
{
825
832
Some ( expr)
@@ -828,16 +835,18 @@ fn expr_as_cast_to_usize<'tcx>(cx: &LateContext<'tcx>, cast_expr: &'tcx Expr<'_>
828
835
}
829
836
}
830
837
831
- // Peel raw casts if the remaining expression can be coerced to it
832
- fn peel_raw_casts < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > , expr_ty : Ty < ' tcx > ) -> & ' tcx Expr < ' tcx > {
833
- if let ExprKind :: Cast ( inner, _) = expr. kind
838
+ // Peel raw casts if the remaining expression can be coerced to it, and whether casts have been
839
+ // peeled or not.
840
+ fn peel_raw_casts < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > , expr_ty : Ty < ' tcx > ) -> ( & ' tcx Expr < ' tcx > , bool ) {
841
+ if !expr. span . from_expansion ( )
842
+ && let ExprKind :: Cast ( inner, _) = expr. kind
834
843
&& let ty:: RawPtr ( target_ty, _) = expr_ty. kind ( )
835
844
&& let inner_ty = cx. typeck_results ( ) . expr_ty ( inner)
836
845
&& let ty:: RawPtr ( inner_target_ty, _) | ty:: Ref ( _, inner_target_ty, _) = inner_ty. kind ( )
837
846
&& target_ty == inner_target_ty
838
847
{
839
- peel_raw_casts ( cx, inner, inner_ty)
848
+ ( peel_raw_casts ( cx, inner, inner_ty) . 0 , true )
840
849
} else {
841
- expr
850
+ ( expr, false )
842
851
}
843
852
}
0 commit comments