Skip to content

Commit 04d4ae7

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 3d0f0e3 commit 04d4ae7

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

clippy_lints/src/macro_use.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::utils::{snippet, span_lint_and_sugg};
1+
use crate::utils::span_lint_and_help;
22
use if_chain::if_chain;
33
use rustc_ast::ast;
4-
use rustc_errors::Applicability;
54
use rustc_lint::{EarlyContext, EarlyLintPass};
65
use rustc_session::{declare_lint_pass, declare_tool_lint};
76
use rustc_span::edition::Edition;
@@ -12,21 +11,21 @@ declare_clippy_lint! {
1211
/// **Why is this bad?** Since the Rust 2018 edition you can import
1312
/// macro's directly, this is considered idiomatic.
1413
///
15-
/// **Known problems:** This lint does not generate an auto-applicable suggestion.
14+
/// **Known problems:** None.
1615
///
1716
/// **Example:**
1817
/// ```rust
1918
/// #[macro_use]
2019
/// use lazy_static;
2120
/// ```
22-
pub MACRO_USE_IMPORTS,
21+
pub MACRO_USE_IMPORT,
2322
pedantic,
2423
"#[macro_use] is no longer needed"
2524
}
2625

27-
declare_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
26+
declare_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]);
2827

29-
impl EarlyLintPass for MacroUseImports {
28+
impl EarlyLintPass for MacroUseImport {
3029
fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) {
3130
if_chain! {
3231
if ecx.sess.opts.edition == Edition::Edition2018;
@@ -36,17 +35,24 @@ impl EarlyLintPass for MacroUseImports {
3635
.iter()
3736
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
3837
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-
);
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);
5056
}
5157
}
5258
}

tests/ui/declare_interior_mutable_const.stderr

Lines changed: 6 additions & 18 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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)