]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/macro_use.rs
Auto merge of #5334 - flip1995:backport_remerge, r=flip1995
[rust.git] / clippy_lints / src / macro_use.rs
1 use crate::utils::{snippet, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_ast::ast;
4 use rustc_errors::Applicability;
5 use rustc_lint::{EarlyContext, EarlyLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::edition::Edition;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for `#[macro_use] use...`.
11     ///
12     /// **Why is this bad?** Since the Rust 2018 edition you can import
13     /// macro's directly, this is considered idiomatic.
14     ///
15     /// **Known problems:** This lint does not generate an auto-applicable suggestion.
16     ///
17     /// **Example:**
18     /// ```rust
19     /// #[macro_use]
20     /// use lazy_static;
21     /// ```
22     pub MACRO_USE_IMPORTS,
23     pedantic,
24     "#[macro_use] is no longer needed"
25 }
26
27 declare_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
28
29 impl EarlyLintPass for MacroUseImports {
30     fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) {
31         if_chain! {
32             if ecx.sess.opts.edition == Edition::Edition2018;
33             if let ast::ItemKind::Use(use_tree) = &item.kind;
34             if let Some(mac_attr) = item
35                 .attrs
36                 .iter()
37                 .find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
38             then {
39                 let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition";
40                 let help = format!("use {}::<macro name>", snippet(ecx, use_tree.span, "_"));
41                 span_lint_and_sugg(
42                     ecx,
43                     MACRO_USE_IMPORTS,
44                     mac_attr.span,
45                     msg,
46                     "remove the attribute and import the macro directly, try",
47                     help,
48                     Applicability::HasPlaceholders,
49                 );
50             }
51         }
52     }
53 }