Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kornelski opened this issue Apr 13, 2024 · 2 comments · Fixed by #13893
Closed

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

kornelski opened this issue Apr 13, 2024 · 2 comments · Fixed by #13893
Labels
A-lint Area: New lints

Comments

@kornelski
Copy link
Contributor

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 kornelski added the A-lint Area: New lints label Apr 13, 2024
@smoelius
Copy link
Contributor

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

@kornelski
Copy link
Contributor Author

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

rust-lang/rust#123940

github-merge-queue bot pushed a commit that referenced this issue Feb 26, 2025
Fixes #12674, i.e., adds a lint to flag `Path`s printed with `{:?}`.

Nits are welcome.

changelog: Add `unnecessary_debug_formatting` lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants