Skip to content

Commit 4bde11f

Browse files
committed
warn on macro_use attr
cargo dev update lints fixed suggestion, check edition, ran `tests/ui/update-all-references.sh` fixed failing tests with update-references.sh warn on macro_use attr (issue #5179) fixed suggestion, check edition, ran `tests/ui/update-all-references.sh` fixed failing tests with update-references.sh update-references.sh for missing-doc-impl.rs use if_chain cargo dev fmt
1 parent 0f4a3fe commit 4bde11f

8 files changed

+101
-20
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 358 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 359 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,
@@ -1011,6 +1013,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10111013
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
10121014
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10131015
store.register_late_pass(|| box wildcard_imports::WildcardImports);
1016+
store.register_early_pass(|| box macro_use::MacroUseImport);
10141017

10151018
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10161019
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1077,6 +1080,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10771080
LintId::of(&literal_representation::LARGE_DIGIT_GROUPS),
10781081
LintId::of(&loops::EXPLICIT_INTO_ITER_LOOP),
10791082
LintId::of(&loops::EXPLICIT_ITER_LOOP),
1083+
LintId::of(&macro_use::MACRO_USE_IMPORT),
10801084
LintId::of(&matches::SINGLE_MATCH_ELSE),
10811085
LintId::of(&methods::FILTER_MAP),
10821086
LintId::of(&methods::FILTER_MAP_NEXT),

clippy_lints/src/macro_use.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::utils::span_lint_and_help;
2+
use if_chain::if_chain;
3+
use rustc_ast::ast;
4+
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use rustc_span::edition::Edition;
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Checks for `#[macro_use] use...`.
10+
///
11+
/// **Why is this bad?** Since the Rust 2018 edition you can import
12+
/// macro's directly, this is considered idiomatic.
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
/// ```rust
18+
/// #[macro_use]
19+
/// use lazy_static;
20+
/// ```
21+
pub MACRO_USE_IMPORT,
22+
pedantic,
23+
"#[macro_use] is no longer needed"
24+
}
25+
26+
declare_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]);
27+
28+
impl EarlyLintPass for MacroUseImport {
29+
fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) {
30+
if_chain! {
31+
if ecx.sess.opts.edition == Edition::Edition2018;
32+
if let ast::ItemKind::Use(use_tree) = &item.kind;
33+
if let Some(mac_attr) = item
34+
.attrs
35+
.iter()
36+
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
37+
then {
38+
let msg = "`macro_use` attribute's are no longer needed in the Rust 2018 edition";
39+
let help = format!(
40+
"remove the attribute and import the macro directly `use {}::<macro name>`",
41+
use_tree
42+
.clone()
43+
.into_inner()
44+
.prefix
45+
.segments
46+
.iter()
47+
.enumerate()
48+
.map(|(i, s)| if i == 0 {
49+
s.ident.to_string()
50+
} else {
51+
format!("::{}", s.ident)
52+
})
53+
.collect::<String>(),
54+
);
55+
span_lint_and_help(ecx, MACRO_USE_IMPORT, mac_attr.span, msg, &help);
56+
}
57+
}
58+
}
59+
}

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; 358] = [
9+
pub const ALL_LINTS: [Lint; 359] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1015,6 +1015,13 @@ pub const ALL_LINTS: [Lint; 358] = [
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/declare_interior_mutable_const.stderr

+6-18
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,19 @@ error: a `const` item should never be interior mutable
4545
--> $DIR/declare_interior_mutable_const.rs:44:5
4646
|
4747
LL | const INPUT: T;
48-
| ^^^^^^^^^^^^^-^
49-
| |
50-
| consider requiring `T` to be `Copy`
48+
| ^^^^^^^^^^^^^^^
5149

5250
error: a `const` item should never be interior mutable
5351
--> $DIR/declare_interior_mutable_const.rs:47:5
5452
|
5553
LL | const ASSOC: Self::NonCopyType;
56-
| ^^^^^^^^^^^^^-----------------^
57-
| |
58-
| consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5955

6056
error: a `const` item should never be interior mutable
6157
--> $DIR/declare_interior_mutable_const.rs:51:5
6258
|
6359
LL | const AN_INPUT: T = Self::INPUT;
64-
| ^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^
65-
| |
66-
| consider requiring `T` to be `Copy`
60+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6761

6862
error: a `const` item should never be interior mutable
6963
--> $DIR/declare_interior_mutable_const.rs:16:9
@@ -80,9 +74,7 @@ error: a `const` item should never be interior mutable
8074
--> $DIR/declare_interior_mutable_const.rs:60:5
8175
|
8276
LL | const SELF_2: Self;
83-
| ^^^^^^^^^^^^^^----^
84-
| |
85-
| consider requiring `Self` to be `Copy`
77+
| ^^^^^^^^^^^^^^^^^^^
8678

8779
error: a `const` item should never be interior mutable
8880
--> $DIR/declare_interior_mutable_const.rs:81:5
@@ -94,17 +86,13 @@ error: a `const` item should never be interior mutable
9486
--> $DIR/declare_interior_mutable_const.rs:84:5
9587
|
9688
LL | const U_SELF: U = U::SELF_2;
97-
| ^^^^^^^^^^^^^^-^^^^^^^^^^^^^
98-
| |
99-
| consider requiring `U` to be `Copy`
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10090

10191
error: a `const` item should never be interior mutable
10292
--> $DIR/declare_interior_mutable_const.rs:87:5
10393
|
10494
LL | const T_ASSOC: T::NonCopyType = T::ASSOC;
105-
| ^^^^^^^^^^^^^^^--------------^^^^^^^^^^^^
106-
| |
107-
| consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10896

10997
error: aborting due to 13 previous errors
11098

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

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

0 commit comments

Comments
 (0)