Skip to content

Declarative macros: warn on unreachable macro rule #130222

@Kriskras99

Description

@Kriskras99

Code

#[macro_export]
macro_rules! example {
    ($lit:expr) => {{
        {}
    }};
    ($lit:literal) => {{
        unreachable!()
    }};
}

fn main() {
    example!(1)
}

Current output

Builds without any warnings, and the resulting binary runs without a panic.

Desired output

A warning telling me that the second macro rule is unreachable, because expr captures the same and more than literal.

Rationale and extra context

I think it's probably impossible to solve this for the general case, but I think it's a very helpful warning for users new to declarative macros.

Other cases

No response

Rust Version

rustc 1.83.0-nightly (0ee7cb5e3 2024-09-10)
binary: rustc
commit-hash: 0ee7cb5e3633502d9a90a85c3c367eccd59a0aba
commit-date: 2024-09-10
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions