@@ -97,30 +97,43 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
97
97
///
98
98
/// Turns
99
99
///
100
- /// `format_args!("Hello, {}! {}", "World", 123)`
100
+ /// `format_args!("Hello, {}! {} {} ", "World", 123, x )`
101
101
///
102
102
/// into
103
103
///
104
- /// `format_args!("Hello, World! {}", 123 )`.
104
+ /// `format_args!("Hello, World! 123 {}", x )`.
105
105
fn inline_literals ( mut fmt : Cow < ' _ , FormatArgs > ) -> Cow < ' _ , FormatArgs > {
106
106
let mut was_inlined = vec ! [ false ; fmt. arguments. all_args( ) . len( ) ] ;
107
107
let mut inlined_anything = false ;
108
108
109
109
for i in 0 ..fmt. template . len ( ) {
110
110
let FormatArgsPiece :: Placeholder ( placeholder) = & fmt. template [ i] else { continue } ;
111
111
let Ok ( arg_index) = placeholder. argument . index else { continue } ;
112
+
113
+ let mut literal = None ;
114
+
112
115
if let FormatTrait :: Display = placeholder. format_trait
113
116
&& placeholder. format_options == Default :: default ( )
114
117
&& let arg = fmt. arguments . all_args ( ) [ arg_index] . expr . peel_parens_and_refs ( )
115
118
&& let ExprKind :: Lit ( lit) = arg. kind
116
- && let token:: LitKind :: Str | token:: LitKind :: StrRaw ( _) = lit. kind
117
- && let Ok ( LitKind :: Str ( s, _) ) = LitKind :: from_token_lit ( lit)
118
119
{
120
+ if let token:: LitKind :: Str | token:: LitKind :: StrRaw ( _) = lit. kind
121
+ && let Ok ( LitKind :: Str ( s, _) ) = LitKind :: from_token_lit ( lit)
122
+ {
123
+ literal = Some ( s) ;
124
+ } else if let token:: LitKind :: Integer = lit. kind
125
+ && let Ok ( LitKind :: Int ( n, _) ) = LitKind :: from_token_lit ( lit)
126
+ {
127
+ literal = Some ( Symbol :: intern ( & n. to_string ( ) ) ) ;
128
+ }
129
+ }
130
+
131
+ if let Some ( literal) = literal {
119
132
// Now we need to mutate the outer FormatArgs.
120
133
// If this is the first time, this clones the outer FormatArgs.
121
134
let fmt = fmt. to_mut ( ) ;
122
135
// Replace the placeholder with the literal.
123
- fmt. template [ i] = FormatArgsPiece :: Literal ( s ) ;
136
+ fmt. template [ i] = FormatArgsPiece :: Literal ( literal ) ;
124
137
was_inlined[ arg_index] = true ;
125
138
inlined_anything = true ;
126
139
}
0 commit comments