@@ -3,7 +3,8 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
33use clippy_utils:: sugg:: has_enclosing_paren;
44use clippy_utils:: ty:: { implements_trait, is_manually_drop, peel_mid_ty_refs} ;
55use clippy_utils:: {
6- expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy , ExprUseNode ,
6+ expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, peel_middle_ty_refs, DefinedTy ,
7+ ExprUseNode ,
78} ;
89use core:: mem;
910use rustc_ast:: util:: parser:: { PREC_PREFIX , PREC_UNAMBIGUOUS } ;
@@ -175,13 +176,15 @@ struct StateData<'tcx> {
175176 adjusted_ty : Ty < ' tcx > ,
176177}
177178
179+ #[ derive( Debug ) ]
178180struct DerefedBorrow {
179181 count : usize ,
180182 msg : & ' static str ,
181183 stability : TyCoercionStability ,
182184 for_field_access : Option < Symbol > ,
183185}
184186
187+ #[ derive( Debug ) ]
185188enum State {
186189 // Any number of deref method calls.
187190 DerefMethod {
@@ -744,7 +747,7 @@ fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> boo
744747 }
745748}
746749
747- #[ derive( Clone , Copy ) ]
750+ #[ derive( Clone , Copy , Debug ) ]
748751enum TyCoercionStability {
749752 Deref ,
750753 Reborrow ,
@@ -1042,16 +1045,28 @@ fn report<'tcx>(
10421045 return ;
10431046 }
10441047
1045- let ( prefix, precedence) = if let Some ( mutability) = mutability
1046- && !typeck. expr_ty ( expr) . is_ref ( )
1048+ let ty = typeck. expr_ty ( expr) ;
1049+
1050+ // `&&[T; N]`, or `&&..&[T; N]` (src) cannot coerce to `&[T]` (dst).
1051+ if let ty:: Ref ( _, dst, _) = data. adjusted_ty . kind ( )
1052+ && dst. is_slice ( )
10471053 {
1048- let prefix = match mutability {
1049- Mutability :: Not => "&" ,
1050- Mutability :: Mut => "&mut " ,
1051- } ;
1052- ( prefix, PREC_PREFIX )
1053- } else {
1054- ( "" , 0 )
1054+ let ( src, n_src_refs) = peel_middle_ty_refs ( ty) ;
1055+ if n_src_refs >= 2 && src. is_array ( ) {
1056+ return ;
1057+ }
1058+ }
1059+
1060+ let ( prefix, precedence) = match mutability {
1061+ Some ( mutability) if !ty. is_ref ( ) => {
1062+ let prefix = match mutability {
1063+ Mutability :: Not => "&" ,
1064+ Mutability :: Mut => "&mut " ,
1065+ } ;
1066+ ( prefix, PREC_PREFIX )
1067+ } ,
1068+ None if !ty. is_ref ( ) && data. adjusted_ty . is_ref ( ) => ( "&" , 0 ) ,
1069+ _ => ( "" , 0 ) ,
10551070 } ;
10561071 span_lint_hir_and_then (
10571072 cx,
0 commit comments