rust-lang / rust-lang/rust-clippy

Suggest better repeated macro param declaration pattern

Open
#9,959 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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 #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)*)
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the bad_macro_param_pattern proposal and its macro_rules! examples, including the repeated expression pattern and optional trailing comma. Define the supported bad patterns and expected suggestions before implementing the lint; done means the named patterns are detected with suitable fixes and formatting concerns are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.