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 better repeated macro param declaration pattern #9959

Open
nyurik opened this issue Nov 26, 2022 · 0 comments
Open

Suggest better repeated macro param declaration pattern #9959

nyurik opened this issue Nov 26, 2022 · 0 comments
Labels
A-lint Area: New lints

Comments

@nyurik
Copy link
Contributor

nyurik commented Nov 26, 2022

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)*)
    }
}
@nyurik nyurik added the A-lint Area: New lints label Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant