Skip to content

Commit a65e7ca

Browse files
committed
Refactor impl std::fmt::Display for Token to use write!() to flatten token lists
This fixes clippy "warning: `format!(..)` appended to existing `String`".
1 parent 99812ab commit a65e7ca

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/refs/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,22 @@ impl std::fmt::Display for Token {
283283
///
284284
/// `format!("{}", parse_ref(<input string>))` should result in the original input string.
285285
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
286-
fn flatten(ts: &[Token]) -> String {
287-
ts.iter().fold(String::new(), |mut st, t| {
288-
st.push_str(&format!("{t}"));
289-
st
290-
})
286+
fn flatten(f: &mut std::fmt::Formatter<'_>, ts: &[Token]) -> std::fmt::Result {
287+
for t in ts {
288+
write!(f, "{t}")?;
289+
}
290+
Ok(())
291291
}
292292
match self {
293293
Token::Literal(s) => {
294294
write!(f, "{}", s.replace('\\', r"\\").replace('$', r"\$"))
295295
}
296296
Token::Ref(ts) => {
297-
let refcontent = flatten(ts);
298-
write!(f, "${{{refcontent}}}")
297+
write!(f, "${{")?;
298+
flatten(f, ts)?;
299+
write!(f, "}}")
299300
}
300-
Token::Combined(ts) => write!(f, "{}", flatten(ts)),
301+
Token::Combined(ts) => flatten(f, ts),
301302
}
302303
}
303304
}

0 commit comments

Comments
 (0)