-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5230 - DevinR528:macro-use, r=flip1995
Macro use --- changelog: This lint enforces Rust 2018 idiom of importing macro's directly without `#[macro_use]` fixes #5179 .
- Loading branch information
Showing
8 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::utils::{snippet, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_ast::ast; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::edition::Edition; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for `#[macro_use] use...`. | ||
/// | ||
/// **Why is this bad?** Since the Rust 2018 edition you can import | ||
/// macro's directly, this is considered idiomatic. | ||
/// | ||
/// **Known problems:** This lint does not generate an auto-applicable suggestion. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// #[macro_use] | ||
/// use lazy_static; | ||
/// ``` | ||
pub MACRO_USE_IMPORT, | ||
pedantic, | ||
"#[macro_use] is no longer needed" | ||
} | ||
|
||
declare_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]); | ||
|
||
impl EarlyLintPass for MacroUseImport { | ||
fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) { | ||
if_chain! { | ||
if ecx.sess.opts.edition == Edition::Edition2018; | ||
if let ast::ItemKind::Use(use_tree) = &item.kind; | ||
if let Some(mac_attr) = item | ||
.attrs | ||
.iter() | ||
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string())); | ||
then { | ||
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition"; | ||
let help = format!("use {}::<macro name>", snippet(ecx, use_tree.span, "_")); | ||
span_lint_and_sugg( | ||
ecx, | ||
MACRO_USE_IMPORT, | ||
mac_attr.span, | ||
msg, | ||
"remove the attribute and import the macro directly, try", | ||
help, | ||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// compile-flags: --edition 2018 | ||
#![warn(clippy::macro_use_import)] | ||
|
||
use std::collections::HashMap; | ||
#[macro_use] | ||
use std::prelude; | ||
|
||
fn main() { | ||
let _ = HashMap::<u8, u8>::new(); | ||
println!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: `macro_use` attributes are no longer needed in the Rust 2018 edition | ||
--> $DIR/macro_use_import.rs:5:1 | ||
| | ||
LL | #[macro_use] | ||
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use std::prelude::<macro name>` | ||
| | ||
= note: `-D clippy::macro-use-import` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|