Skip to content

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

@killercup

Description

@killercup

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-styleLint: Belongs in the style lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions