format! nested inside another formatting macro
ID |
rust.performance.format_in_format_args |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
The inner format! allocates a String that the outer formatting macro immediately copies and discards. Inline the inner arguments into the outer format string so the value is written once, with no intermediate allocation.
Rationale
The inner format! allocates a String that the outer formatting macro immediately copies and discards. Inline the inner arguments into the outer format string so the value is written once, with no intermediate allocation.
The following code illustrates the pattern detected by this rule:
use std::fmt::Write;
fn report(id: u32, name: &str) -> String {
// FLAGGED: format! nested inside another formatting macro
println!("{}", format!("{} {}", id, name));
// FLAGGED: format! nested inside another formatting macro
let line = format!("[{}]", format!("{}-{}", id, name));
line
}