@@ -34,7 +34,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
3434 if let FormatArgsPiece :: Placeholder ( placeholder) = & fmt. template [ i]
3535 && let FormatTrait :: Display | FormatTrait :: Debug = & placeholder. format_trait
3636 && let Ok ( arg_index) = placeholder. argument . index
37- && let arg = & fmt. arguments . all_args ( ) [ arg_index] . expr
37+ && let arg = fmt. arguments . all_args ( ) [ arg_index] . expr . peel_parens_and_refs ( )
3838 && let ExprKind :: FormatArgs ( _) = & arg. kind
3939 // Check that this argument is not used by any other placeholders.
4040 && fmt. template . iter ( ) . enumerate ( ) . all ( |( j, p) |
@@ -54,9 +54,14 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
5454 let args = fmt. arguments . all_args_mut ( ) ;
5555 let remaining_args = args. split_off ( arg_index + 1 ) ;
5656 let old_arg_offset = args. len ( ) ;
57- let fmt2 = args. pop ( ) . unwrap ( ) . expr . into_inner ( ) ; // The inner FormatArgs.
58- let ExprKind :: FormatArgs ( fmt2) = fmt2. kind else { unreachable ! ( ) } ;
59- let mut fmt2 = fmt2. into_inner ( ) ;
57+ let mut fmt2 = & mut args. pop ( ) . unwrap ( ) . expr ; // The inner FormatArgs.
58+ let fmt2 = loop { // Unwrap the Expr to get to the FormatArgs.
59+ match & mut fmt2. kind {
60+ ExprKind :: Paren ( inner) | ExprKind :: AddrOf ( BorrowKind :: Ref , _, inner) => fmt2 = inner,
61+ ExprKind :: FormatArgs ( fmt2) => break fmt2,
62+ _ => unreachable ! ( ) ,
63+ }
64+ } ;
6065
6166 args. append ( fmt2. arguments . all_args_mut ( ) ) ;
6267 let new_arg_offset = args. len ( ) ;
@@ -78,7 +83,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
7883 let rest = fmt. template . split_off ( i + 1 ) ;
7984 fmt. template . pop ( ) ; // remove the placeholder for the nested fmt args.
8085
81- for piece in fmt2. template {
86+ for piece in fmt2. template . drain ( .. ) {
8287 match piece {
8388 FormatArgsPiece :: Literal ( s) => fmt. template . push ( FormatArgsPiece :: Literal ( s) ) ,
8489 FormatArgsPiece :: Placeholder ( mut p) => {
@@ -119,7 +124,8 @@ fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
119124 let FormatArgsPiece :: Placeholder ( placeholder) = & fmt. template [ i] else { continue } ;
120125 let Ok ( arg_index) = placeholder. argument . index else { continue } ;
121126 if let FormatTrait :: Display = placeholder. format_trait
122- && let ExprKind :: Lit ( lit) = fmt. arguments . all_args ( ) [ arg_index] . expr . kind
127+ && let arg = fmt. arguments . all_args ( ) [ arg_index] . expr . peel_parens_and_refs ( )
128+ && let ExprKind :: Lit ( lit) = arg. kind
123129 && let token:: LitKind :: Str | token:: LitKind :: StrRaw ( _) = lit. kind
124130 && let Ok ( LitKind :: Str ( s, _) ) = LitKind :: from_token_lit ( lit)
125131 {
0 commit comments