]> git.lizzy.rs Git - rust.git/blob - src/docs/crate_in_macro_def.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / crate_in_macro_def.txt
1 ### What it does
2 Checks for use of `crate` as opposed to `$crate` in a macro definition.
3
4 ### Why is this bad?
5 `crate` refers to the macro call's crate, whereas `$crate` refers to the macro definition's
6 crate. Rarely is the former intended. See:
7 https://doc.rust-lang.org/reference/macros-by-example.html#hygiene
8
9 ### Example
10 ```
11 #[macro_export]
12 macro_rules! print_message {
13     () => {
14         println!("{}", crate::MESSAGE);
15     };
16 }
17 pub const MESSAGE: &str = "Hello!";
18 ```
19 Use instead:
20 ```
21 #[macro_export]
22 macro_rules! print_message {
23     () => {
24         println!("{}", $crate::MESSAGE);
25     };
26 }
27 pub const MESSAGE: &str = "Hello!";
28 ```
29
30 Note that if the use of `crate` is intentional, an `allow` attribute can be applied to the
31 macro definition, e.g.:
32 ```
33 #[allow(clippy::crate_in_macro_def)]
34 macro_rules! ok { ... crate::foo ... }
35 ```