@@ -800,7 +800,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
800800 let mut is_raw_ptr = false ;
801801 let mut is_ref = false ;
802802 let mut is_destructuring_assignment = false ;
803+ let mut is_nested_deref = false ;
803804 if let Some ( inner) = inner {
805+ is_nested_deref =
806+ matches ! ( inner. kind, hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , _) ) ;
804807 let typck_result = self . infcx . tcx . typeck ( self . mir_def_id ( ) ) ;
805808 if let Some ( inner_type) = typck_result. node_type_opt ( inner. hir_id ) {
806809 if matches ! ( inner_type. kind( ) , ty:: RawPtr ( ..) ) {
@@ -822,21 +825,25 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
822825 }
823826 // If the `inner` is a raw pointer, do not suggest removing the "*", see #126863
824827 // FIXME: need to check whether the assigned object can be a raw pointer, see `tests/ui/borrowck/issue-20801.rs`.
825- if !is_raw_ptr && ( !is_destructuring_pattern_move || is_ref) {
828+ if is_raw_ptr {
829+ return ;
830+ }
831+
832+ if !is_destructuring_pattern_move || is_ref {
826833 err. span_suggestion_verbose (
827834 span. with_hi ( span. lo ( ) + BytePos ( 1 ) ) ,
828835 "consider removing the dereference here" ,
829836 String :: new ( ) ,
830837 Applicability :: MaybeIncorrect ,
831838 ) ;
832- } else if !is_raw_ptr && !is_destructuring_assignment {
839+ } else if !is_destructuring_assignment && !is_nested_deref {
833840 err. span_suggestion_verbose (
834841 span. shrink_to_lo ( ) ,
835842 "consider borrowing here" ,
836843 '&' ,
837844 Applicability :: MaybeIncorrect ,
838845 ) ;
839- } else if !is_raw_ptr {
846+ } else {
840847 err. span_help (
841848 span,
842849 "destructuring assignment cannot borrow from this expression; consider using a `let` binding instead" ,
0 commit comments