From 53df6383b7fafaeb3cc381889b2ecaea80617a4a Mon Sep 17 00:00:00 2001 From: Devin R Date: Wed, 26 Feb 2020 07:40:31 -0500 Subject: [PATCH] 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 --- clippy_lints/src/macro_use.rs | 40 +++++++++++-------- .../ui/declare_interior_mutable_const.stderr | 24 +++-------- tests/ui/macro_use_import.rs | 11 +++++ tests/ui/macro_use_import.stderr | 11 +++++ 4 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 tests/ui/macro_use_import.rs create mode 100644 tests/ui/macro_use_import.stderr diff --git a/clippy_lints/src/macro_use.rs b/clippy_lints/src/macro_use.rs index b1345d0b751a..e8cb97006ab0 100644 --- a/clippy_lints/src/macro_use.rs +++ b/clippy_lints/src/macro_use.rs @@ -1,7 +1,6 @@ -use crate::utils::{snippet, span_lint_and_sugg}; +use crate::utils::span_lint_and_help; 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; @@ -12,21 +11,21 @@ declare_clippy_lint! { /// **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. + /// **Known problems:** None. /// /// **Example:** /// ```rust /// #[macro_use] /// use lazy_static; /// ``` - pub MACRO_USE_IMPORTS, + pub MACRO_USE_IMPORT, pedantic, "#[macro_use] is no longer needed" } -declare_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]); +declare_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]); -impl EarlyLintPass for MacroUseImports { +impl EarlyLintPass for MacroUseImport { fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) { if_chain! { if ecx.sess.opts.edition == Edition::Edition2018; @@ -36,17 +35,24 @@ impl EarlyLintPass for MacroUseImports { .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 {}::", snippet(ecx, use_tree.span, "_")); - span_lint_and_sugg( - ecx, - MACRO_USE_IMPORTS, - mac_attr.span, - msg, - "remove the attribute and import the macro directly, try", - help, - Applicability::HasPlaceholders, - ); + 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 {}::`", + use_tree + .clone() + .into_inner() + .prefix + .segments + .iter() + .enumerate() + .map(|(i, s)| if i == 0 { + s.ident.to_string() + } else { + format!("::{}", s.ident) + }) + .collect::(), + ); + span_lint_and_help(ecx, MACRO_USE_IMPORT, mac_attr.span, msg, &help); } } } diff --git a/tests/ui/declare_interior_mutable_const.stderr b/tests/ui/declare_interior_mutable_const.stderr index 6a9a57361f9f..3f8b58f08c59 100644 --- a/tests/ui/declare_interior_mutable_const.stderr +++ b/tests/ui/declare_interior_mutable_const.stderr @@ -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 `>::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 @@ -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 @@ -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 `>::NonCopyType` to be `Copy` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 13 previous errors diff --git a/tests/ui/macro_use_import.rs b/tests/ui/macro_use_import.rs new file mode 100644 index 000000000000..33ce2b524ef7 --- /dev/null +++ b/tests/ui/macro_use_import.rs @@ -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::::new(); + println!(); +} diff --git a/tests/ui/macro_use_import.stderr b/tests/ui/macro_use_import.stderr new file mode 100644 index 000000000000..531297ed4721 --- /dev/null +++ b/tests/ui/macro_use_import.stderr @@ -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::` + +error: aborting due to previous error +