diff --git a/clippy_lints/src/macro_use.rs b/clippy_lints/src/macro_use.rs index e8cb97006ab0..07dd57a3288c 100644 --- a/clippy_lints/src/macro_use.rs +++ b/clippy_lints/src/macro_use.rs @@ -1,6 +1,7 @@ -use crate::utils::span_lint_and_help; +use crate::utils::{snippet, span_lint_and_sugg}; 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; @@ -35,24 +36,17 @@ impl EarlyLintPass for MacroUseImport { .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 {}::`", - 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); + 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_IMPORT, + mac_attr.span, + msg, + "remove the attribute and import the macro directly, try", + help, + Applicability::HasPlaceholders, + ); } } } diff --git a/macro_use_import b/macro_use_import new file mode 100755 index 000000000000..61d3a827f1f7 Binary files /dev/null and b/macro_use_import differ diff --git a/tests/ui/macro_use_import.stderr b/tests/ui/macro_use_import.stderr index 531297ed4721..1d86ba584411 100644 --- a/tests/ui/macro_use_import.stderr +++ b/tests/ui/macro_use_import.stderr @@ -1,11 +1,10 @@ -error: `macro_use` attribute's are no longer needed in the Rust 2018 edition +error: `macro_use` attributes are no longer needed in the Rust 2018 edition --> $DIR/macro_use_import.rs:5:1 | LL | #[macro_use] - | ^^^^^^^^^^^^ + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use std::prelude::` | = 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 diff --git a/tests/ui/macro_use_import.stdout b/tests/ui/macro_use_import.stdout new file mode 100644 index 000000000000..e69de29bb2d1