Skip to content

Commit 23d2b21

Browse files
committed
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 .
2 parents ac2e10a + 597e02d commit 23d2b21

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ Released 2018-09-13
12091209
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
12101210
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
12111211
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
1212+
[`macro_use_import`]: https://rust-lang.github.io/rust-clippy/master/index.html#macro_use_import
12121213
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
12131214
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
12141215
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
77

8-
[There are 359 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 360 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

1010
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1111

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub mod let_underscore;
234234
pub mod lifetimes;
235235
pub mod literal_representation;
236236
pub mod loops;
237+
pub mod macro_use;
237238
pub mod main_recursion;
238239
pub mod map_clone;
239240
pub mod map_unit_fn;
@@ -599,6 +600,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
599600
&loops::WHILE_IMMUTABLE_CONDITION,
600601
&loops::WHILE_LET_LOOP,
601602
&loops::WHILE_LET_ON_ITERATOR,
603+
&macro_use::MACRO_USE_IMPORT,
602604
&main_recursion::MAIN_RECURSION,
603605
&map_clone::MAP_CLONE,
604606
&map_unit_fn::OPTION_MAP_UNIT_FN,
@@ -1012,6 +1014,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10121014
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
10131015
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10141016
store.register_late_pass(|| box wildcard_imports::WildcardImports);
1017+
store.register_early_pass(|| box macro_use::MacroUseImport);
10151018

10161019
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10171020
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1079,6 +1082,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10791082
LintId::of(&literal_representation::LARGE_DIGIT_GROUPS),
10801083
LintId::of(&loops::EXPLICIT_INTO_ITER_LOOP),
10811084
LintId::of(&loops::EXPLICIT_ITER_LOOP),
1085+
LintId::of(&macro_use::MACRO_USE_IMPORT),
10821086
LintId::of(&matches::SINGLE_MATCH_ELSE),
10831087
LintId::of(&methods::FILTER_MAP),
10841088
LintId::of(&methods::FILTER_MAP_NEXT),

clippy_lints/src/macro_use.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_IMPORT,
23+
pedantic,
24+
"#[macro_use] is no longer needed"
25+
}
26+
27+
declare_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]);
28+
29+
impl EarlyLintPass for MacroUseImport {
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_IMPORT,
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+
}

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 359] = [
9+
pub const ALL_LINTS: [Lint; 360] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1015,6 +1015,13 @@ pub const ALL_LINTS: [Lint; 359] = [
10151015
deprecation: None,
10161016
module: "float_literal",
10171017
},
1018+
Lint {
1019+
name: "macro_use_import",
1020+
group: "pedantic",
1021+
desc: "#[macro_use] is no longer needed",
1022+
deprecation: None,
1023+
module: "macro_use",
1024+
},
10181025
Lint {
10191026
name: "main_recursion",
10201027
group: "style",

tests/ui/macro_use_import.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: --edition 2018
2+
#![warn(clippy::macro_use_import)]
3+
4+
use std::collections::HashMap;
5+
#[macro_use]
6+
use std::prelude;
7+
8+
fn main() {
9+
let _ = HashMap::<u8, u8>::new();
10+
println!();
11+
}

tests/ui/macro_use_import.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2+
--> $DIR/macro_use_import.rs:5:1
3+
|
4+
LL | #[macro_use]
5+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use std::prelude::<macro name>`
6+
|
7+
= note: `-D clippy::macro-use-import` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)