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};
10 declare_clippy_lint! {
12 /// Checks for use of `crate` as opposed to `$crate` in a macro definition.
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
22 /// macro_rules! print_message {
24 /// println!("{}", crate::MESSAGE);
27 /// pub const MESSAGE: &str = "Hello!";
32 /// macro_rules! print_message {
34 /// println!("{}", $crate::MESSAGE);
37 /// pub const MESSAGE: &str = "Hello!";
40 /// Note that if the use of `crate` is intentional, an `allow` attribute can be applied to the
41 /// macro definition, e.g.:
43 /// #[allow(clippy::crate_in_macro_def)]
44 /// macro_rules! ok { ... crate::foo ... }
46 #[clippy::version = "1.61.0"]
47 pub CRATE_IN_MACRO_DEF,
49 "using `crate` in a macro definition"
51 declare_lint_pass!(CrateInMacroDef => [CRATE_IN_MACRO_DEF]);
53 impl EarlyLintPass for CrateInMacroDef {
54 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
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);
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,
75 fn is_macro_export(attr: &Attribute) -> bool {
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;
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() {
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);
101 if let TokenTree::Delimited(_, _, tts) = &curr {
102 let span = contains_unhygienic_crate_reference(tts);
107 prev_is_dollar = is_token(&curr, &TokenKind::Dollar);
112 fn is_crate_keyword(tt: &TokenTree) -> Option<Span> {
114 if let TokenTree::Token(Token { kind: TokenKind::Ident(symbol, _), span }) = tt;
115 if symbol.as_str() == "crate";
116 then { Some(*span) } else { None }
120 fn is_token(tt: &TokenTree, kind: &TokenKind) -> bool {
121 if let TokenTree::Token(Token { kind: other, .. }) = tt {