Skip to content

match_like_matches_macro macro gets expanded in suggestion when matching on macro #16015

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::match_like_matches_macro

this code:

use std::any::{type_name, TypeId};

pub struct GetTypeId<T>(T);

impl<T: 'static> GetTypeId<T> {
    pub const VALUE: TypeId = TypeId::of::<T>();
}

#[macro_export]
macro_rules! typeid {
    ($t:ty) => {
        $crate::GetTypeId::<$t>::VALUE
    };
}

const fn same_type<T: 'static, U: 'static>() -> bool {
    match typeid!(T) {
        _ => true,
        _ => false,
    }
}

fn print_if_equal<T: 'static, U: 'static>() {
    if same_type::<T, U>() {
        println!("{} == {}", type_name::<T>(), type_name::<U>());
    } else {
        
    }
}

fn main() {
    print_if_equal::<usize, u32>();
    print_if_equal::<usize, usize>();
}

caused the following diagnostics:

    Checking _45049dfa6ff33ebd29f080109ea274f8636bfbb6 v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.X6Ai7ihNEVse/_45049dfa6ff33ebd29f080109ea274f8636bfbb6)
warning: match expression looks like `matches!` macro
  --> src/main.rs:18:5
   |
18 | /     match typeid!(T) {
19 | |         _ => true,
20 | |         _ => false,
21 | |     }
   | |_____^ help: try: `matches!($crate::GetTypeId::<$t>::VALUE, _)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
   = note: requested on the command line with `--force-warn clippy::match-like-matches-macro`

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

However after applying these diagnostics, the resulting code:

use std::any::{type_name, TypeId};

pub struct GetTypeId<T>(T);

impl<T: 'static> GetTypeId<T> {
    pub const VALUE: TypeId = TypeId::of::<T>();
}

#[macro_export]
macro_rules! typeid {
    ($t:ty) => {
        $crate::GetTypeId::<$t>::VALUE
    };
}

const fn same_type<T: 'static, U: 'static>() -> bool {
    matches!($crate::GetTypeId::<$t>::VALUE, _)
}

fn print_if_equal<T: 'static, U: 'static>() {
    if same_type::<T, U>() {
        println!("{} == {}", type_name::<T>(), type_name::<U>());
    } else {
        
    }
}

fn main() {
    print_if_equal::<usize, u32>();
    print_if_equal::<usize, usize>();
}

no longer compiled:

    Checking _45049dfa6ff33ebd29f080109ea274f8636bfbb6 v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.X6Ai7ihNEVse/_45049dfa6ff33ebd29f080109ea274f8636bfbb6)
error: no rules expected `$`
   --> src/main.rs:18:14
    |
 18 |     matches!($crate::GetTypeId::<$t>::VALUE, _)
    |              ^ no rules expected this token in macro call
    |
note: while trying to match meta-variable `$expression:expr`
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/macros/mod.rs:435:6
    |
435 |     ($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
    |      ^^^^^^^^^^^^^^^^

error: could not compile `_45049dfa6ff33ebd29f080109ea274f8636bfbb6` (bin "_45049dfa6ff33ebd29f080109ea274f8636bfbb6" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_45049dfa6ff33ebd29f080109ea274f8636bfbb6` (bin "_45049dfa6ff33ebd29f080109ea274f8636bfbb6") due to 1 previous error

Version:

rustc 1.93.0-nightly (6a884ad1b 2025-11-02)
binary: rustc
commit-hash: 6a884ad1b502fe48307d363858510702429fc735
commit-date: 2025-11-02
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.3

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 appliedT-macrosType: Issues with macros and macro expansion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions