dylint icon indicating copy to clipboard operation
dylint copied to clipboard

Add format_concat_args lint to optimize string formatting

Open ginzahatemi opened this issue 8 months ago • 2 comments

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:

  1. When all placeholders use the Display trait (default {} format)
  2. 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

ginzahatemi avatar May 15 '25 11:05 ginzahatemi