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

Suggest named parameters for complex/long format! calls #633

Open
killercup opened this issue Feb 7, 2016 · 2 comments
Open

Suggest named parameters for complex/long format! calls #633

killercup opened this issue Feb 7, 2016 · 2 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group

Comments

@killercup
Copy link
Member

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.

@mcarton mcarton added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-AST Type: Requires working with the AST A-lint Area: New lints labels Feb 25, 2016
@mcarton mcarton added the L-style Lint: Belongs in the style lint group label Jul 25, 2016
@Alexendoo Alexendoo removed the T-AST Type: Requires working with the AST label Sep 26, 2022
@RobbieMcKinstry
Copy link

RobbieMcKinstry commented Jul 31, 2023

Hello Rust friends! 👋🏻

I think this issue can be closed as "completed" 🎉 I believe the lint uninlined_format_args implements @killercup 's original feature request. I'm not sure when it was introduced, but it appears to me to be equivalent to the requested feature. Feel free to correct me. 😸

@Centri3
Copy link
Member

Centri3 commented Aug 1, 2023

Kind of. It was about naming the parameters using =, but now that we can inline format arguments, it's not as big of an issue. It's still applicable when it's not a single segment path to a local though, like original_name = get_original_name(self) (I believe you can do that?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group
Projects
None yet
Development

No branches or pull requests

5 participants