]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/crate_in_macro_def.rs
Update clippy_lints/src/crate_in_macro_def.rs
[rust.git] / clippy_lints / src / crate_in_macro_def.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use rustc_ast::ast::{AttrKind, Attribute, Item, ItemKind};
3 use rustc_ast::token::{Token, TokenKind};
4 use rustc_ast::tokenstream::{TokenStream, TokenTree};
5 use rustc_errors::Applicability;
6 use rustc_lint::{EarlyContext, EarlyLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::{symbol::sym, Span};
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for use of `crate` as opposed to `$crate` in a macro definition.
13     ///
14     /// ### Why is this bad?
15     /// `crate` refers to the macro call's crate, whereas `$crate` refers to the macro definition's
16     /// crate. Rarely is the former intended. See:
17     /// https://doc.rust-lang.org/reference/macros-by-example.html#hygiene
18     ///
19     /// ### Example
20     /// ```rust
21     /// #[macro_export]
22     /// macro_rules! print_message {
23     ///     () => {
24     ///         println!("{}", crate::MESSAGE);
25     ///     };
26     /// }
27     /// pub const MESSAGE: &str = "Hello!";
28     /// ```
29     /// Use instead:
30     /// ```rust
31     /// #[macro_export]
32     /// macro_rules! print_message {
33     ///     () => {
34     ///         println!("{}", $crate::MESSAGE);
35     ///     };
36     /// }
37     /// pub const MESSAGE: &str = "Hello!";
38     /// ```
39     ///
40     /// Note that if the use of `crate` is intentional, an `allow` attribute can be applied to the
41     /// macro definition, e.g.:
42     /// ```rust,ignore
43     /// #[allow(clippy::crate_in_macro_def)]
44     /// macro_rules! ok { ... crate::foo ... }
45     /// ```
46     #[clippy::version = "1.61.0"]
47     pub CRATE_IN_MACRO_DEF,
48     correctness,
49     "using `crate` in a macro definition"
50 }
51 declare_lint_pass!(CrateInMacroDef => [CRATE_IN_MACRO_DEF]);
52
53 impl EarlyLintPass for CrateInMacroDef {
54     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
55         if_chain! {
56             if item.attrs.iter().any(is_macro_export);
57             if let ItemKind::MacroDef(macro_def) = &item.kind;
58             let tts = macro_def.body.inner_tokens();
59             if let Some(span) = contains_unhygienic_crate_reference(&tts);
60             then {
61                 span_lint_and_sugg(
62                     cx,
63                     CRATE_IN_MACRO_DEF,
64                     span,
65                     "`crate` references the macro call's crate",
66                     "if reference to the macro definition's crate is intended, use",
67                     String::from("$crate"),
68                     Applicability::MachineApplicable,
69                 );
70             }
71         }
72     }
73 }
74
75 fn is_macro_export(attr: &Attribute) -> bool {
76     if_chain! {
77         if let AttrKind::Normal(attr_item, _) = &attr.kind;
78         if let [segment] = attr_item.path.segments.as_slice();
79         if segment.ident.name == sym::macro_export;
80         then {
81             true
82         } else {
83             false
84         }
85     }
86 }
87
88 fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
89     let mut prev_is_dollar = false;
90     let mut cursor = tts.trees();
91     while let Some(curr) = cursor.next() {
92         if_chain! {
93             if !prev_is_dollar;
94             if let Some(span) = is_crate_keyword(&curr);
95             if let Some(next) = cursor.look_ahead(0);
96             if is_token(next, &TokenKind::ModSep);
97             then {
98                 return Some(span);
99             }
100         }
101         if let TokenTree::Delimited(_, _, tts) = &curr {
102             let span = contains_unhygienic_crate_reference(tts);
103             if span.is_some() {
104                 return span;
105             }
106         }
107         prev_is_dollar = is_token(&curr, &TokenKind::Dollar);
108     }
109     None
110 }
111
112 fn is_crate_keyword(tt: &TokenTree) -> Option<Span> {
113     if_chain! {
114         if let TokenTree::Token(Token { kind: TokenKind::Ident(symbol, _), span }) = tt;
115         if symbol.as_str() == "crate";
116         then { Some(*span) } else { None }
117     }
118 }
119
120 fn is_token(tt: &TokenTree, kind: &TokenKind) -> bool {
121     if let TokenTree::Token(Token { kind: other, .. }) = tt {
122         kind == other
123     } else {
124         false
125     }
126 }