dylint
dylint copied to clipboard
Add format_concat_args lint to optimize string formatting
This PR addresses #1601 by implementing a new lint that detects cases where format!() could be
replaced with concat!() for improved performance.
Problem
When all arguments to a format!() macro are compile-time constants and use
the standard Display formatting trait, the formatting can be done at compile time
with concat!() instead of being deferred to runtime.
Solution
This lint analyzes format string placeholders and their arguments to detect:
- When all placeholders use the Display trait (default
{}format) - When all arguments are const-evaluatable (compile-time constants)
When both conditions are met, it suggests replacing with an equivalent concat!()
expression.
Examples
// Will trigger the lint:
format!("hello {}", "world") // suggests: concat!("hello ", "world")
format!("{}/file.txt", env!("CARGO_MANIFEST_DIR")) // suggests: concat!(env!("CARGO_MANIFEST_DIR"), "/file.txt")
// Will not trigger:
format!("debug: {:?}", some_struct) // Uses Debug trait
format!("value: {}", runtime_variable) // Not const-evaluatable
Fixes #1601