Skip to content

Commit c4c3d91

Browse files
committed
Don't produce empty literal pieces in inline_literals.
1 parent 90b6588 commit c4c3d91

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,32 @@ impl<'hir> LoweringContext<'_, 'hir> {
8989
let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
9090
let mut inlined_anything = false;
9191

92-
for i in 0..fmt.template.len() {
93-
let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
94-
let Ok(arg_index) = placeholder.argument.index else { continue };
92+
let mut i = 0;
9593

96-
let mut literal = None;
97-
98-
if let FormatTrait::Display = placeholder.format_trait
94+
while i < fmt.template.len() {
95+
if let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i]
96+
&& let Ok(arg_index) = placeholder.argument.index
97+
&& let FormatTrait::Display = placeholder.format_trait
9998
&& placeholder.format_options == Default::default()
10099
&& let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
101100
&& let ExprKind::Lit(lit) = arg.kind
101+
&& let Some(literal) = self.try_inline_lit(lit)
102102
{
103-
literal = self.try_inline_lit(lit);
104-
}
105103

106-
if let Some(literal) = literal {
107104
// Now we need to mutate the outer FormatArgs.
108105
// If this is the first time, this clones the outer FormatArgs.
109106
let fmt = fmt.to_mut();
110107
// Replace the placeholder with the literal.
111-
fmt.template[i] = FormatArgsPiece::Literal(literal);
108+
if literal.is_empty() {
109+
fmt.template.remove(i);
110+
} else {
111+
fmt.template[i] = FormatArgsPiece::Literal(literal);
112+
i += 1;
113+
}
112114
was_inlined[arg_index] = true;
113115
inlined_anything = true;
116+
} else {
117+
i += 1;
114118
}
115119
}
116120

0 commit comments

Comments
 (0)