Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::crate_in_macro_def: `crate` references the macro call's crate
What it meansChecks for usage of crate as opposed to $crate in a macro definition. It is in the suspicious group and warns by default.
What you see
warning: `crate` references the macro call's crate --> main.rs:6:24 | 6 | println!("{}", crate::MESSAGE); | ^^^^^ help: to reference the macro definition's crate, use: `$crate` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#crate_in_macro_def = note: `#[warn(clippy::crate_in_macro_def)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
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
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
#[macro_export]
macro_rules! print_message {
() => {
println!("{}", crate::MESSAGE);
};
}
pub const MESSAGE: &str = "Hello!";
Write this instead
#[macro_export]
macro_rules! print_message {
() => {
println!("{}", $crate::MESSAGE);
};
}
pub const MESSAGE: &str = "Hello!";
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::crate_in_macro_def)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
crate_in_macro_def = "allow"One run
cargo clippy -- -A clippy::crate-in-macro-defThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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 ... }
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- cast_slice_from_raw_partsChecks for a raw slice being cast to a slice pointer
- confusing_method_to_numeric_castcasting function pointer `u16::max` to `usize`
- const_is_emptythis expression always evaluates to true
- crosspointer_transmuteChecks for transmutes between a type T and *T
- declare_interior_mutable_constnamed constant with interior mutability
- deprecated_clippy_cfg_attr`feature = "cargo-clippy"` was replaced by `clippy`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::cast_slice_from_raw_partsChecks for a raw slice being cast to a slice pointerRead it next →Did this answer your question?