Skip to content

Commit

Permalink
Remove string allocation in relative import formatting (#7743)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 1, 2023
1 parent 2838f7a commit d8a6279
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/ruff_python_formatter/src/statement/stmt_import_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit d8a6279

Please sign in to comment.