Skip to content

Lint suggestion: Mismatched macro repetition operator #16138

@theemathas

Description

@theemathas

What it does

In a macro_rules! definition, there are three different repetition operators that can be used: *, +, ?. Rust accepts the code even if the repetition operator in the macro matcher is different from the repetition operator in the macro transcriber. This lint would warn against such a mismatch.

Advantage

  • Having mismatched repetition operators can be confusing, since the kind of repetition operator used in the macro transcriber is ignored, and therefore can be misleading.
  • Mismatched repetition operators can be a sign of a bug, where the macro author intended to refer to a different "repetition group".

Drawbacks

No response

Example

macro_rules! my_macro {
    ($($i:ident)?) => {
        $($i)*
    }
}

Could be written as:

macro_rules! my_macro {
    ($($i:ident)?) => {
        $($i)?
    }
}

Comparison with existing lints

No response

Additional Context

No response

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