RS.CLIPPY.CRATE_IN_MACRO_DEF

Using `crate` in a macro definition

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: crate_in_macro_def. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for usage of crate as opposed to $crate in a macro definition.

Why is this bad?

crate refers to the macro call's crate, whereas $crate refers to the macro definition's crate. Rarely is the former intended. See: https://doc.rust-lang.org/reference/macros-by-example.html#hygiene

Example

#[macro_export]
macro_rules! print_message {
    () => {
        println!("{}", crate::MESSAGE);
    };
}
pub const MESSAGE: &str = "Hello!";

Use instead:

#[macro_export]
macro_rules! print_message {
    () => {
        println!("{}", $crate::MESSAGE);
    };
}
pub const MESSAGE: &str = "Hello!";

Note that if the use of crate is intentional, an allow attribute can be applied to the macro definition, e.g.:

#[allow(clippy::crate_in_macro_def)]
macro_rules! ok { ... crate::foo ... }