rust-lang / rust-lang/rust-clippy

`equatable-if-let` puts bindings outside of macro inside macro definition

Open
#16,154 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-suggestion-causes-error L-suggestion T-macros
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Using the following flags

--force-warn clippy::equatable-if-let

this code:

//@ run-pass

// Check that it is possible to resolve, in the value namespace,
// to an `enum` variant through a type alias. This includes `Self`.
// Type qualified syntax `<Type>::Variant` also works when syntactically valid.

#[derive(Debug, PartialEq, Eq)]
enum Foo {
    Bar(i32),
    Baz { i: i32 },
    Qux,
}

type FooAlias = Foo;
type OptionAlias = Option<i32>;

macro_rules! check_pat {
    ($x:expr, $p:pat) => {
        assert!(if let $p = $x { true } else { false });
    };
}

impl Foo {
    fn bar() -> Self {
        let x = Self::Bar(3);
        assert_eq!(x, <Self>::Bar(3));
        check_pat!(x, Self::Bar(3));
        x
    }
}

fn main() {}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.QlfkGs76BA87/icemaker_clippyfix_tempdir.lb9ooZZJhcpZ/_a)
warning: this pattern matching can be expressed using equality
  --> src/main.rs:19:20
   |
19 |         assert!(if let $p = $x { true } else { false });
   |                    ^^^^^^^^^^^ help: try: `x == Self::Bar(3)`
...
27 |         check_pat!(x, Self::Bar(3));
   |         --------------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#equatable_if_let
   = note: requested on the command line with `--force-warn clippy::equatable-if-let`
   = note: this warning originates in the macro `check_pat` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `_a` (bin "_a") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s

However after applying these diagnostics, the resulting code:

//@ run-pass

// Check that it is possible to resolve, in the value namespace,
// to an `enum` variant through a type alias. This includes `Self`.
// Type qualified syntax `<Type>::Variant` also works when syntactically valid.

#[derive(Debug, PartialEq, Eq)]
enum Foo {
    Bar(i32),
    Baz { i: i32 },
    Qux,
}

type FooAlias = Foo;
type OptionAlias = Option<i32>;

macro_rules! check_pat {
    ($x:expr, $p:pat) => {
        assert!(if x == Self::Bar(3) { true } else { false });
    };
}

impl Foo {
    fn bar() -> Self {
        let x = Self::Bar(3);
        assert_eq!(x, <Self>::Bar(3));
        check_pat!(x, Self::Bar(3));
        x
    }
}

fn main() {}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.QlfkGs76BA87/icemaker_clippyfix_tempdir.lb9ooZZJhcpZ/_a)
error[E0425]: cannot find value `x` in this scope
  --> src/main.rs:19:20
   |
19 |         assert!(if x == Self::Bar(3) { true } else { false });
   |                    ^ not found in this scope
...
27 |         check_pat!(x, Self::Bar(3));
   |         --------------------------- in this macro invocation
   |
help: an identifier with the same name exists, but is not accessible due to macro hygiene
  --> src/main.rs:25:13
   |
25 |         let x = Self::Bar(3);
   |             ^
   = note: this error originates in the macro `check_pat` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0425`.
error: could not compile `_a` (bin "_a") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a" test) due to 1 previous error

Version:

rustc 1.93.0-nightly (8a3a6bdb6 2025-11-29)
binary: rustc
commit-hash: 8a3a6bdb68b4d4c9ed922840808b02015741331e
commit-date: 2025-11-29
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

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 equatable-if-let lint and reproduce the reported macro_rules! case using the code in the issue. Trace how the diagnostic suggestion handles bindings from macro arguments, then verify that the suggested result preserves macro hygiene and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.