Skip to content

Commit 5baeef2

Browse files
Replace inefficient push_str(&format!(...)) calls with write! macro
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent ab6049b commit 5baeef2

8 files changed

Lines changed: 108 additions & 95 deletions

File tree

‎cli/src/commands/doc.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use crate::artifacts::ArtifactManager;
66
use crate::exit_code::ExitCode;
7+
use std::fmt::Write;
78
use std::path::{Path, PathBuf};
89
use techscript_package_manager::DocExtractor;
910

@@ -55,14 +56,15 @@ pub fn execute(path_str: Option<&str>) -> ExitCode {
5556

5657
// Build structured output
5758
let mut md_content = String::new();
58-
md_content.push_str(&format!(
59+
let _ = write!(
60+
md_content,
5961
"# API Documentation — {}\n\n",
6062
path.file_name().unwrap_or_default().to_string_lossy()
61-
));
63+
);
6264

6365
for item in &doc_items {
64-
md_content.push_str(&format!("## {}\n\n", item.name));
65-
md_content.push_str(&format!("{}\n\n", item.doc));
66+
let _ = write!(md_content, "## {}\n\n", item.name);
67+
let _ = write!(md_content, "{}\n\n", item.doc);
6668
md_content.push_str("---\n\n");
6769
}
6870

‎cli/src/commands/migrate.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! Automatically migrates legacy TechScript 1.0.8 code to TechScript 2.0 canonical syntax.
44
55
use crate::exit_code::ExitCode;
6+
use std::fmt::Write;
67
use std::path::PathBuf;
78

89
pub fn execute(path_str: Option<&str>) -> ExitCode {
@@ -263,7 +264,7 @@ fn replace_call(source: &str, prefix: &str, keyword: &str) -> String {
263264
remaining = &remaining[pos + full.len()..];
264265
if let Some(close) = remaining.find(')') {
265266
let args = &remaining[..close];
266-
result.push_str(&format!("{keyword} {args}"));
267+
let _ = write!(result, "{keyword} {args}");
267268
remaining = &remaining[close + 1..];
268269
} else {
269270
result.push_str(remaining);

‎cli/src/crash.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! and write structured crash reports.
55
66
use crate::exit_code::ExitCode;
7+
use std::fmt::Write;
78
use std::panic;
89
use std::path::{Path, PathBuf};
910
use std::time::SystemTime;
@@ -33,12 +34,12 @@ impl CrashReport {
3334
out.push_str(
3435
"================================================================================\n",
3536
);
36-
out.push_str(&format!("Compiler Version: {}\n", self.compiler_version));
37-
out.push_str(&format!("CLI Command: {}\n", self.command));
38-
out.push_str(&format!("Timestamp: {}\n", self.timestamp));
39-
out.push_str(&format!("OS Info: {}\n", self.os_info));
37+
let _ = write!(out, "Compiler Version: {}\n", self.compiler_version);
38+
let _ = write!(out, "CLI Command: {}\n", self.command);
39+
let _ = write!(out, "Timestamp: {}\n", self.timestamp);
40+
let _ = write!(out, "OS Info: {}\n", self.os_info);
4041
if let Some(file) = &self.source_file {
41-
out.push_str(&format!("Active File: {}\n", file));
42+
let _ = write!(out, "Active File: {}\n", file);
4243
}
4344
out.push_str(
4445
"--------------------------------------------------------------------------------\n",

‎cli/src/diagnostics.rs‎

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use colored::Colorize;
88
use std::sync::Arc;
99
use std::time::Duration;
1010

11+
use std::fmt::Write;
1112
use techscript_common::{FileId, SourceManager};
1213
use techscript_errors::{Diagnostic, DiagnosticLevel, ErrorCode};
1314

@@ -229,29 +230,32 @@ impl<'a> DiagnosticRenderer<'a> {
229230

230231
if self.output == DiagnosticOutput::Colored {
231232
if header.is_empty() {
232-
out.push_str(&format!(
233+
let _ = write!(
234+
out,
233235
"{}: {}\n",
234236
diag.severity.colored_label(),
235237
diag.message.bold()
236-
));
238+
);
237239
} else {
238-
out.push_str(&format!(
240+
let _ = write!(
241+
out,
239242
"{}[{}]: {}\n",
240243
diag.severity.colored_label(),
241244
header.white(),
242245
diag.message.bold()
243-
));
246+
);
244247
}
245248
} else {
246249
if header.is_empty() {
247-
out.push_str(&format!("{}: {}\n", diag.severity.label(), diag.message));
250+
let _ = write!(out, "{}: {}\n", diag.severity.label(), diag.message);
248251
} else {
249-
out.push_str(&format!(
252+
let _ = write!(
253+
out,
250254
"{}[{}]: {}\n",
251255
diag.severity.label(),
252256
header,
253257
diag.message
254-
));
258+
);
255259
}
256260
}
257261

@@ -262,26 +266,21 @@ impl<'a> DiagnosticRenderer<'a> {
262266

263267
let file_ref = format!(" --> {}:{}:{}", file.path().display(), line, col);
264268
if self.output == DiagnosticOutput::Colored {
265-
out.push_str(&format!("{}\n", file_ref.cyan()));
269+
let _ = write!(out, "{}\n", file_ref.cyan());
266270
} else {
267-
out.push_str(&format!("{}\n", file_ref));
271+
let _ = write!(out, "{}\n", file_ref);
268272
}
269273

270274
// Source line
271275
if let Some(line_text) = file.line_content(line) {
272276
let line_num = format!("{:>4}", line);
273277
let bar = "|";
274278
if self.output == DiagnosticOutput::Colored {
275-
out.push_str(&format!("{} {}\n", line_num.cyan(), bar.cyan()));
276-
out.push_str(&format!(
277-
"{} {} {}\n",
278-
line_num.cyan(),
279-
bar.cyan(),
280-
line_text
281-
));
279+
let _ = write!(out, "{} {}\n", line_num.cyan(), bar.cyan());
280+
let _ = write!(out, "{} {} {}\n", line_num.cyan(), bar.cyan(), line_text);
282281
} else {
283-
out.push_str(&format!("{} {}\n", line_num, bar));
284-
out.push_str(&format!("{} {} {}\n", line_num, bar, line_text));
282+
let _ = write!(out, "{} {}\n", line_num, bar);
283+
let _ = write!(out, "{} {} {}\n", line_num, bar, line_text);
285284
}
286285

287286
// Caret underline — Unicode-safe (count chars, not bytes)
@@ -298,13 +297,9 @@ impl<'a> DiagnosticRenderer<'a> {
298297
format!("{}{}", spaces, carets)
299298
};
300299
if self.output == DiagnosticOutput::Colored {
301-
out.push_str(&format!(
302-
" {} {}\n",
303-
bar.cyan(),
304-
caret_line.red().bold()
305-
));
300+
let _ = write!(out, " {} {}\n", bar.cyan(), caret_line.red().bold());
306301
} else {
307-
out.push_str(&format!(" {} {}\n", bar, caret_line));
302+
let _ = write!(out, " {} {}\n", bar, caret_line);
308303
}
309304
}
310305
}
@@ -313,54 +308,54 @@ impl<'a> DiagnosticRenderer<'a> {
313308
// Related diagnostics (help, notes)
314309
for related in &diag.related {
315310
if self.output == DiagnosticOutput::Colored {
316-
out.push_str(&format!(
311+
let _ = write!(
312+
out,
317313
" {}: {}\n",
318314
related.severity.colored_label(),
319315
related.message
320-
));
316+
);
321317
} else {
322-
out.push_str(&format!(
323-
" {}: {}\n",
324-
related.severity.label(),
325-
related.message
326-
));
318+
let _ = write!(out, " {}: {}\n", related.severity.label(), related.message);
327319
}
328320
}
329321

330322
// Fix suggestions
331323
for suggestion in &diag.suggestions {
332324
if self.output == DiagnosticOutput::Colored {
333-
out.push_str(&format!(
325+
let _ = write!(
326+
out,
334327
" {}: {} → `{}`\n",
335328
"suggestion".green().bold(),
336329
suggestion.message,
337330
suggestion.replacement.green()
338-
));
331+
);
339332
} else {
340-
out.push_str(&format!(
333+
let _ = write!(
334+
out,
341335
" suggestion: {} → `{}`\n",
342336
suggestion.message, suggestion.replacement
343-
));
337+
);
344338
}
345339
}
346340

347341
// Documentation link if code is present
348342
if let Some(code) = diag.code {
349343
if self.output == DiagnosticOutput::Colored {
350-
out.push_str(&format!(
344+
let _ = write!(
345+
out,
351346
" {}: For more details see: {}\n",
352347
"note".cyan().bold(),
353348
format!(
354349
"https://github.com/Tcode-Motion/techscript/blob/main/docs/errors.md#{:?}",
355350
code
356351
)
357352
.underline()
358-
));
353+
);
359354
} else {
360-
out.push_str(&format!(
355+
let _ = write!(out,
361356
" note: For more details see: https://github.com/Tcode-Motion/techscript/blob/main/docs/errors.md#{:?}\n",
362357
code
363-
));
358+
);
364359
}
365360
}
366361

‎cli/src/profiler.rs‎

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use colored::Colorize;
1010

1111
use crate::events::{CompilationEvent, EventListener};
1212
use crate::logging::format_duration;
13+
use std::fmt::Write;
1314

1415
// ─── StageTimings ────────────────────────────────────────────────────────────
1516

@@ -97,75 +98,81 @@ impl TimingProfiler {
9798
let total: Duration = self.stages.iter().map(|s| s.duration).sum();
9899
let mut out = String::new();
99100

100-
out.push_str(&format!(
101+
let _ = write!(
102+
out,
101103
"{}\n",
102104
"─── Compilation Timings ────────────────────────────────".dimmed()
103-
));
104-
out.push_str(&format!(
105+
);
106+
let _ = write!(
107+
out,
105108
"{:<24} {:>10} {}\n",
106109
"Stage".bold(),
107110
"Duration".bold(),
108111
"Items".bold()
109-
));
110-
out.push_str(&format!("{}\n", "─".repeat(50).dimmed()));
112+
);
113+
let _ = write!(out, "{}\n", "─".repeat(50).dimmed());
111114

112115
for s in &self.stages {
113-
out.push_str(&format!(
116+
let _ = write!(
117+
out,
114118
"{:<24} {:>10} {} {}\n",
115119
s.stage,
116120
format_duration(s.duration).cyan().to_string(),
117121
s.item_count,
118122
s.item_label.dimmed()
119-
));
123+
);
120124
}
121125

122-
out.push_str(&format!("{}\n", "─".repeat(50).dimmed()));
123-
out.push_str(&format!(
126+
let _ = write!(out, "{}\n", "─".repeat(50).dimmed());
127+
let _ = write!(
128+
out,
124129
"{:<24} {:>10}\n\n",
125130
"Total".bold(),
126131
format_duration(total).green().bold().to_string()
127-
));
132+
);
128133

129134
// Memory section
130135
let mem = &self.memory;
131136
if mem.peak_memory_bytes > 0 || mem.ast_node_count > 0 {
132-
out.push_str(&format!(
137+
let _ = write!(
138+
out,
133139
"{}\n",
134140
"─── Memory Statistics ──────────────────────────────────".dimmed()
135-
));
136-
out.push_str(&format!(
141+
);
142+
let _ = write!(
143+
out,
137144
"{:<24} {:>10}\n",
138145
"Peak memory",
139146
format_bytes(mem.peak_memory_bytes).cyan().to_string()
140-
));
147+
);
141148
if mem.ir_size_bytes > 0 {
142-
out.push_str(&format!(
149+
let _ = write!(
150+
out,
143151
"{:<24} {:>10}\n",
144152
"IR module",
145153
format_bytes(mem.ir_size_bytes).cyan().to_string()
146-
));
154+
);
147155
}
148156
if mem.bytecode_size_bytes > 0 {
149-
out.push_str(&format!(
157+
let _ = write!(
158+
out,
150159
"{:<24} {:>10}\n",
151160
"Bytecode",
152161
format_bytes(mem.bytecode_size_bytes).cyan().to_string()
153-
));
162+
);
154163
}
155164
if mem.ast_node_count > 0 {
156-
out.push_str(&format!("{:<24} {:>10}\n", "AST nodes", mem.ast_node_count));
165+
let _ = write!(out, "{:<24} {:>10}\n", "AST nodes", mem.ast_node_count);
157166
}
158167
if mem.function_count > 0 {
159-
out.push_str(&format!(
160-
"{:<24} {:>10}\n",
161-
"IR functions", mem.function_count
162-
));
168+
let _ = write!(out, "{:<24} {:>10}\n", "IR functions", mem.function_count);
163169
}
164170
if mem.basic_block_count > 0 {
165-
out.push_str(&format!(
171+
let _ = write!(
172+
out,
166173
"{:<24} {:>10}\n",
167174
"Basic blocks", mem.basic_block_count
168-
));
175+
);
169176
}
170177
}
171178

0 commit comments

Comments
 (0)