Remove string allocation in relative import formatting (#7743)

This commit is contained in:
Charlie Marsh 2023-10-01 14:15:43 -04:00 committed by GitHub
parent 2838f7af98
commit d8a6279fe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,16 +20,17 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
range: _,
} = item;
let level_str = level
.map(|level| ".".repeat(level as usize))
.unwrap_or(String::default());
write!(
f,
[
token("from"),
space(),
text(&level_str, None),
format_with(|f| {
for _ in 0..level.unwrap_or(0) {
token(".").fmt(f)?;
}
Ok(())
}),
module.as_ref().map(AsFormat::format),
space(),
token("import"),