Skip to content

Suggest better repeated macro param declaration pattern #9959

Open
@nyurik

Description

@nyurik

What it does

Try to catch bad macro param patterns, and suggest better ones:

  • Replace <anything>, $($e:expr),* with <anything> $(, $e:expr)* $(,)?
    • this must also replace all usages of <anything>, $($e,)* with <anything> $(, $e)*
  • Add missing trailing comma - can be fixed by adding $(,)?. See also Lint macro definitions that don't allow trailing commas #1848
  • ? some other bad patterns ?

Lint Name

bad_macro_param_pattern

Category

suspicious

Advantage

  • keeps macro declarations more consistent
  • catches common errors like using format-like macro: my_macro!("{}", foo) not being replaceable by my_macro!("{foo}") (i.e. it is no longer requires a single comma to always be present)

Drawbacks

possible formatting issues on auto-fix

Example

macro_rules! my_macro {
    ($fmt:literal, $($e:expr),*) => {
        println!($fmt, $($e,)*)
    }
}

Could be written as:

macro_rules! my_macro {
    ($fmt:literal $(, $e:expr)* $(,)?) => {
        println!($fmt $(, $e)*)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions