rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Paths printed with {:?} instead of .display()

Open kornelski opened this issue 1 year ago • 2 comments

What it does

Warn when a Debug representation of Path is used in format! or println!, instead of path.display().

Advantage

Rust doesn't guarantee how Debug formatting looks like, and it could change in the future. For printing paths there's the dedicated .display() method.

Drawbacks

Not every Debug print of a Path is incorrect: it may be used in dbg!(), or when a PathBuf is a field in a struct that is Debug-printed as a whole.

Example

let path = Path::new("…");
println!("The path is {:?}", path);

Could be written as:

let path = Path::new("…");
println!("The path is {}", path.display());

kornelski avatar Apr 13 '24 19:04 kornelski

At the risk of feature creep, it might be good to warn about println!("... {} ...", path.to_string_lossy()) also.

smoelius avatar Apr 14 '24 14:04 smoelius

Another reason is that I'm removing support for Debug from the executables I build:

https://github.com/rust-lang/rust/pull/123940

kornelski avatar Apr 14 '24 19:04 kornelski