Skip to content

Commit

Permalink
warn on macro_use attr
Browse files Browse the repository at this point in the history
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 rust-lang#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
  • Loading branch information
DevinR528 committed Mar 3, 2020
1 parent b96c3ca commit d5211d9
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ Released 2018-09-13
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
[`macro_use_import`]: https://rust-lang.github.io/rust-clippy/master/index.html#macro_use_import
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

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

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

Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub mod let_underscore;
pub mod lifetimes;
pub mod literal_representation;
pub mod loops;
pub mod macro_use;
pub mod main_recursion;
pub mod map_clone;
pub mod map_unit_fn;
Expand Down Expand Up @@ -599,6 +600,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&loops::WHILE_IMMUTABLE_CONDITION,
&loops::WHILE_LET_LOOP,
&loops::WHILE_LET_ON_ITERATOR,
&macro_use::MACRO_USE_IMPORT,
&main_recursion::MAIN_RECURSION,
&map_clone::MAP_CLONE,
&map_unit_fn::OPTION_MAP_UNIT_FN,
Expand Down Expand Up @@ -1011,6 +1013,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
store.register_late_pass(|| box wildcard_imports::WildcardImports);
store.register_early_pass(|| box macro_use::MacroUseImport);

store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
Expand Down Expand Up @@ -1077,6 +1080,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&literal_representation::LARGE_DIGIT_GROUPS),
LintId::of(&loops::EXPLICIT_INTO_ITER_LOOP),
LintId::of(&loops::EXPLICIT_ITER_LOOP),
LintId::of(&macro_use::MACRO_USE_IMPORT),
LintId::of(&matches::SINGLE_MATCH_ELSE),
LintId::of(&methods::FILTER_MAP),
LintId::of(&methods::FILTER_MAP_NEXT),
Expand Down
59 changes: 59 additions & 0 deletions clippy_lints/src/macro_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::utils::span_lint_and_help;
use if_chain::if_chain;
use rustc_ast::ast;
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:** None.
///
/// **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` attribute's are no longer needed in the Rust 2018 edition";
let help = format!(
"remove the attribute and import the macro directly `use {}::<macro name>`",
use_tree
.clone()
.into_inner()
.prefix
.segments
.iter()
.enumerate()
.map(|(i, s)| if i == 0 {
s.ident.to_string()
} else {
format!("::{}", s.ident)
})
.collect::<String>(),
);
span_lint_and_help(ecx, MACRO_USE_IMPORT, mac_attr.span, msg, &help);
}
}
}
}
9 changes: 8 additions & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;

// begin lint list, do not remove this comment, it’s used in `update_lints`
pub const ALL_LINTS: [Lint; 358] = [
pub const ALL_LINTS: [Lint; 359] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
Expand Down Expand Up @@ -1015,6 +1015,13 @@ pub const ALL_LINTS: [Lint; 358] = [
deprecation: None,
module: "float_literal",
},
Lint {
name: "macro_use_import",
group: "pedantic",
desc: "#[macro_use] is no longer needed",
deprecation: None,
module: "macro_use",
},
Lint {
name: "main_recursion",
group: "style",
Expand Down
24 changes: 6 additions & 18 deletions tests/ui/declare_interior_mutable_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,19 @@ error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:44:5
|
LL | const INPUT: T;
| ^^^^^^^^^^^^^-^
| |
| consider requiring `T` to be `Copy`
| ^^^^^^^^^^^^^^^

error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:47:5
|
LL | const ASSOC: Self::NonCopyType;
| ^^^^^^^^^^^^^-----------------^
| |
| consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:51:5
|
LL | const AN_INPUT: T = Self::INPUT;
| ^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^^
| |
| consider requiring `T` to be `Copy`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:16:9
Expand All @@ -80,9 +74,7 @@ error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:60:5
|
LL | const SELF_2: Self;
| ^^^^^^^^^^^^^^----^
| |
| consider requiring `Self` to be `Copy`
| ^^^^^^^^^^^^^^^^^^^

error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:81:5
Expand All @@ -94,17 +86,13 @@ error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:84:5
|
LL | const U_SELF: U = U::SELF_2;
| ^^^^^^^^^^^^^^-^^^^^^^^^^^^^
| |
| consider requiring `U` to be `Copy`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: a `const` item should never be interior mutable
--> $DIR/declare_interior_mutable_const.rs:87:5
|
LL | const T_ASSOC: T::NonCopyType = T::ASSOC;
| ^^^^^^^^^^^^^^^--------------^^^^^^^^^^^^
| |
| consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 13 previous errors

11 changes: 11 additions & 0 deletions tests/ui/macro_use_import.rs
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!();
}
11 changes: 11 additions & 0 deletions tests/ui/macro_use_import.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `macro_use` attribute's are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_import.rs:5:1
|
LL | #[macro_use]
| ^^^^^^^^^^^^
|
= note: `-D clippy::macro-use-import` implied by `-D warnings`
= help: remove the attribute and import the macro directly `use std::prelude::<macro name>`

error: aborting due to previous error

0 comments on commit d5211d9

Please sign in to comment.