@@ -670,19 +670,32 @@ fn lint_wide_pointer<'tcx>(
670670 l : & ' tcx hir:: Expr < ' tcx > ,
671671 r : & ' tcx hir:: Expr < ' tcx > ,
672672) {
673- let ptr_unsized = |mut ty : Ty < ' tcx > | -> Option < ( usize , bool ) > {
673+ let ptr_unsized = |mut ty : Ty < ' tcx > | -> Option < (
674+ /* number of refs */ usize ,
675+ /* modifiers */ String ,
676+ /* is dyn */ bool ,
677+ ) > {
674678 let mut refs = 0 ;
675679 // here we remove any "implicit" references and count the number
676680 // of them to correctly suggest the right number of deref
677681 while let ty:: Ref ( _, inner_ty, _) = ty. kind ( ) {
678682 ty = * inner_ty;
679683 refs += 1 ;
680684 }
681- match ty. kind ( ) {
682- ty:: RawPtr ( ty, _) => ( !ty. is_sized ( cx. tcx , cx. param_env ) )
683- . then ( || ( refs, matches ! ( ty. kind( ) , ty:: Dynamic ( _, _, ty:: Dyn ) ) ) ) ,
684- _ => None ,
685- }
685+
686+ // get the inner type of a pointer (or akin)
687+ let mut modifiers = String :: new ( ) ;
688+ ty = match ty. kind ( ) {
689+ ty:: RawPtr ( ty, _) => * ty,
690+ ty:: Adt ( def, args) if cx. tcx . is_diagnostic_item ( sym:: NonNull , def. did ( ) ) => {
691+ modifiers. push_str ( ".as_ptr()" ) ;
692+ args. type_at ( 0 )
693+ }
694+ _ => return None ,
695+ } ;
696+
697+ ( !ty. is_sized ( cx. tcx , cx. param_env ) )
698+ . then ( || ( refs, modifiers, matches ! ( ty. kind( ) , ty:: Dynamic ( _, _, ty:: Dyn ) ) ) )
686699 } ;
687700
688701 // the left and right operands can have references, remove any explicit references
@@ -696,10 +709,10 @@ fn lint_wide_pointer<'tcx>(
696709 return ;
697710 } ;
698711
699- let Some ( ( l_ty_refs, l_inner_ty_is_dyn) ) = ptr_unsized ( l_ty) else {
712+ let Some ( ( l_ty_refs, l_modifiers , l_inner_ty_is_dyn) ) = ptr_unsized ( l_ty) else {
700713 return ;
701714 } ;
702- let Some ( ( r_ty_refs, r_inner_ty_is_dyn) ) = ptr_unsized ( r_ty) else {
715+ let Some ( ( r_ty_refs, r_modifiers , r_inner_ty_is_dyn) ) = ptr_unsized ( r_ty) else {
703716 return ;
704717 } ;
705718
@@ -724,6 +737,9 @@ fn lint_wide_pointer<'tcx>(
724737 let deref_left = & * "*" . repeat ( l_ty_refs) ;
725738 let deref_right = & * "*" . repeat ( r_ty_refs) ;
726739
740+ let l_modifiers = & * l_modifiers;
741+ let r_modifiers = & * r_modifiers;
742+
727743 cx. emit_span_lint (
728744 AMBIGUOUS_WIDE_POINTER_COMPARISONS ,
729745 e. span ,
@@ -733,6 +749,8 @@ fn lint_wide_pointer<'tcx>(
733749 ne,
734750 deref_left,
735751 deref_right,
752+ l_modifiers,
753+ r_modifiers,
736754 left,
737755 middle,
738756 right,
@@ -743,6 +761,8 @@ fn lint_wide_pointer<'tcx>(
743761 ne,
744762 deref_left,
745763 deref_right,
764+ l_modifiers,
765+ r_modifiers,
746766 left,
747767 middle,
748768 right,
@@ -751,6 +771,8 @@ fn lint_wide_pointer<'tcx>(
751771 AmbiguousWidePointerComparisonsAddrSuggestion :: Cast {
752772 deref_left,
753773 deref_right,
774+ l_modifiers,
775+ r_modifiers,
754776 paren_left : if l_ty_refs != 0 { ")" } else { "" } ,
755777 paren_right : if r_ty_refs != 0 { ")" } else { "" } ,
756778 left_before : ( l_ty_refs != 0 ) . then_some ( l_span. shrink_to_lo ( ) ) ,
0 commit comments