rust-lang / rust-lang/rust-clippy
Suggest named parameters for complex/long format! calls
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
I propose a new lint which suggests to use named parameters in format! string, if
- more than 3 parameters are used,
- or one parameter (the same binding) is used more than once
format! is the syntax extension used in println!, panic! and many other string formatting macros. It supports, among a lot of other cool stuff, named parameters. This is described in the fmt docs.
Long strings with lot's of substitutions (or repeated substitutions) become much more readable with named parameters. Just compare:
bail!("\
cannot replace `{}` with `{}`, the source `{}` supports checksums,
but `{}` does not
a lock file compatible with `{}` cannot be generated in this situation
", orig_name, name, supports, no_support, orig_name);
to this:
bail!("\
cannot replace `{orig_name}` with `{new}`, the source `{supporting}` supports checksums,
but `{not_supporting}` does not
a lock file compatible with `{orig_name}` cannot be generated in this situation
", original=orig_name, new=name, supporting=supports, not_supporting=no_support);
The code is from cargo, I added the renames just to show they are supported and to make the substitutions more readable.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Rust fmt documentation and the example in src/cargo/sources/config.rs to understand named formatting parameters and repeated substitutions. Then locate the repository's existing format-macro lint entry point and tests; done means the lint flags calls exceeding three parameters or reusing one binding, with coverage for both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100