summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/crate_in_macro_def.txt
blob: 047e986dee71fae10dd59b6faf35eabbbff50db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
### What it does
Checks for use 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 ... }
```