From d8a6279fe5fadee4f61171ffd1264ae8573bb438 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 1 Oct 2023 14:15:43 -0400 Subject: [PATCH] Remove string allocation in relative import formatting (#7743) --- .../src/statement/stmt_import_from.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs index 660cc1883b..377d34ef62 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs @@ -20,16 +20,17 @@ impl FormatNodeRule 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"),