Skip to content

equatable_if_let ignores repeating macro variables #14548

Open
@mi2ebi

Description

@mi2ebi

Summary

When checking macros equatable_if_let ignores variable repetitions

Reproducer

I tried this code:

macro_rules! generate_script {
    ($($($font:literal)|* => $script:literal),* $(,)?) => {
        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
        pub struct Script(String);
        pub fn script(font: &str) -> Script {
            $(
            if let $($font)|* = font {
                return Script($script.to_string());
            }
            )*
            panic!(
                "unknown font name `\x1b[91m{font}\x1b[m` - please file an issue on the github repo or i'll catch \
                 it in up to three months"
            )
        }
        pub fn all_variants() -> Vec<String> {
            let mut a = vec![];
            $(
            a.extend([$($font),*]);
            )*
            a.iter().map(|v| v.to_string()).collect_vec()
        }
    }
}

I expected to see this happen: Nothing

Instead, this happened:

warning: this pattern matching can be expressed using equality
   --> src\lib.rs:330:16
    |
330 |               if let $($font)|* = font {
    |                  ^^^^^^^^^^^^^^^^^^^^^ help: try: `font == $font`
...
349 | / generate_script! {
350 | |     // check via / ((?!Sans|Serif)[a-zA-Z]+)([ ,]|$).*\n.* \1([ ,]|$)/
351 | |     "Sans" | "Serif" | "Sans Mono" => "",
352 | |     "Sans Adlam" | "Sans Adlam Unjoined" => "Adlam",
...   |
525 | |     "Znamenny Musical Notation" => "Znamenny Musical Notation",
526 | | }
    | |_- in this macro invocation
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#equatable_if_let
    = note: `-W clippy::equatable-if-let` implied by `-W clippy::nursery`
    = help: to override `-W clippy::nursery` add `#[allow(clippy::equatable_if_let)]`
    = note: this warning originates in the macro `generate_script` (in Nightly builds, run with -Z macro-backtrace for more info)

Trying Clippy's suggestion yields this error:

error: variable `font` is still repeating at this depth
   --> src\lib.rs:330:24
    |
330 |             if font == $font {
    |                        ^^^^^

Version

rustc 1.88.0-nightly (00095b3da 2025-04-03)
binary: rustc
commit-hash: 00095b3da4f23d9b3e7a809ac6a4e2b2530df84c
commit-date: 2025-04-03
host: x86_64-pc-windows-msvc
release: 1.88.0-nightly
LLVM version: 20.1.2

Additional Labels

@rustbot label I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-nurseryLint: Currently in the nursery group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions