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

Handle captured identifiers in format strings #5450

Closed
nyurik opened this issue Jul 18, 2022 · 5 comments
Closed

Handle captured identifiers in format strings #5450

nyurik opened this issue Jul 18, 2022 · 5 comments

Comments

@nyurik
Copy link
Contributor

nyurik commented Jul 18, 2022

Since 1.58 Rust allows println!("Hello, {person}!"); style strings (documentations). I would like to propose cargo fmt to automatically convert println!("Hello, {}!", person); and other format!-like calls to the inlined versions, either by default, or, at least initially, as an opt-in.

Original code After cargo fmt Notes
println!("{}", a) println!("{a}") positional in-lining
println!("{a}", a=b) println!("{b}") named param
println!("{a:0$}", width) println!("{a:width$}") width param
println!("{a:.0$}", prec) println!("{a:.prec$}") precision param
@calebcartwright
Copy link
Member

I wonder if rustfix might be a better venue for such transformations

@nyurik
Copy link
Contributor Author

nyurik commented Jul 18, 2022

I wonder if rustfix might be a better venue for such transformations

I just looked at rustfix readme: The magic of rustfix is entirely dependent on the diagnostics implemented in the Rust compiler (and external lints, like clippy).

My understanding is that rustfix won't actually decide anything, but rather use whatever suggestion is made by rustc or clippy. I am not sure this type of fix would belong in the rustc itself. Do you think clippy would be a better target?

@calebcartwright
Copy link
Member

I think clippy would probably be the best place to start, just to see if they happen to already have a lint or if they have one planned. If they do, then that's something rustfix could pick up.

@nyurik
Copy link
Contributor Author

nyurik commented Jul 18, 2022

Thanks @calebcartwright , turns out clippy already has a similar (smaller-scoped) request, so closing in favor of rust-lang/rust-clippy#8368

@nyurik nyurik closed this as completed Jul 18, 2022
@calebcartwright
Copy link
Member

Perfect, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants