@@ -34,7 +34,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
34
34
if let FormatArgsPiece :: Placeholder ( placeholder) = & fmt. template [ i]
35
35
&& let FormatTrait :: Display | FormatTrait :: Debug = & placeholder. format_trait
36
36
&& 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 ( )
38
38
&& let ExprKind :: FormatArgs ( _) = & arg. kind
39
39
// Check that this argument is not used by any other placeholders.
40
40
&& fmt. template . iter ( ) . enumerate ( ) . all ( |( j, p) |
@@ -54,9 +54,14 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
54
54
let args = fmt. arguments . all_args_mut ( ) ;
55
55
let remaining_args = args. split_off ( arg_index + 1 ) ;
56
56
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
+ } ;
60
65
61
66
args. append ( fmt2. arguments . all_args_mut ( ) ) ;
62
67
let new_arg_offset = args. len ( ) ;
@@ -78,7 +83,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
78
83
let rest = fmt. template . split_off ( i + 1 ) ;
79
84
fmt. template . pop ( ) ; // remove the placeholder for the nested fmt args.
80
85
81
- for piece in fmt2. template {
86
+ for piece in fmt2. template . drain ( .. ) {
82
87
match piece {
83
88
FormatArgsPiece :: Literal ( s) => fmt. template . push ( FormatArgsPiece :: Literal ( s) ) ,
84
89
FormatArgsPiece :: Placeholder ( mut p) => {
@@ -119,7 +124,8 @@ fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
119
124
let FormatArgsPiece :: Placeholder ( placeholder) = & fmt. template [ i] else { continue } ;
120
125
let Ok ( arg_index) = placeholder. argument . index else { continue } ;
121
126
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
123
129
&& let token:: LitKind :: Str | token:: LitKind :: StrRaw ( _) = lit. kind
124
130
&& let Ok ( LitKind :: Str ( s, _) ) = LitKind :: from_token_lit ( lit)
125
131
{
0 commit comments