From 288df59b25c04f05d08d817727a8ac9c8d0d6648 Mon Sep 17 00:00:00 2001 From: Devin R Date: Sun, 7 Jun 2020 16:12:35 -0400 Subject: [PATCH] Fix suggestion output, add run-rustfix to test file, stop sorting import segments duh --- clippy_lints/src/macro_use.rs | 35 +++++++++++++------------- tests/ui/macro_use_imports.fixed | 41 +++++++++++++++++++++++++++++++ tests/ui/macro_use_imports.rs | 1 + tests/ui/macro_use_imports.stderr | 16 ++++++------ 4 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 tests/ui/macro_use_imports.fixed diff --git a/clippy_lints/src/macro_use.rs b/clippy_lints/src/macro_use.rs index 089ae79b02c2..7e3ce07254f7 100644 --- a/clippy_lints/src/macro_use.rs +++ b/clippy_lints/src/macro_use.rs @@ -167,32 +167,33 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports { [] => unreachable!("this should never be empty"), [_] => unreachable!("path must have two segments ?"), [root, item] => { - if !check_dup.contains(&item.to_string()) { + if !check_dup.contains(&(*item).to_string()) { used.entry((root.to_string(), span)) - .or_insert(vec![]) + .or_insert_with(|| vec![]) .push(item.to_string()); check_dup.push(item.to_string()); } }, [root, rest @ ..] => { - if !rest.iter().all(|item| !check_dup.contains(&item.to_string())) { - let mut rest = rest.to_vec(); - rest.sort(); - used.entry((root.to_string(), span)) - .or_insert(vec![]) - .push(rest.join("::")); - check_dup.extend(rest.iter().map(ToString::to_string)); - } else { - let mut filtered = rest + if rest.iter().all(|item| !check_dup.contains(&(*item).to_string())) { + let filtered = rest .iter() - .filter(|item| !check_dup.contains(&item.to_string())) - .map(ToString::to_string) + .filter_map(|item| if check_dup.contains(&(*item).to_string()) { + None + } else { + Some(item.to_string()) + }) .collect::>(); - filtered.sort(); - used.entry((root.to_string(), span)) - .or_insert(vec![]) + used.entry(((*root).to_string(), span)) + .or_insert_with(|| vec![]) .push(filtered.join("::")); check_dup.extend(filtered); + } else { + let rest = rest.to_vec(); + used.entry((root.to_string(), span)) + .or_insert_with(|| vec![]) + .push(rest.join("::")); + check_dup.extend(rest.iter().map(ToString::to_string)); } }, } @@ -212,7 +213,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports { // such as `std::prelude::v1::foo` or some other macro that expands to an import. if self.mac_refs.is_empty() { for (span, import) in suggestions { - let help = format!("use {}", import); + let help = format!("use {};", import); span_lint_and_sugg( cx, MACRO_USE_IMPORTS, diff --git a/tests/ui/macro_use_imports.fixed b/tests/ui/macro_use_imports.fixed new file mode 100644 index 000000000000..8034c56b59ad --- /dev/null +++ b/tests/ui/macro_use_imports.fixed @@ -0,0 +1,41 @@ +// compile-flags: --edition 2018 +// aux-build:macro_rules.rs +// aux-build:macro_use_helper.rs +// run-rustfix + +#![allow(clippy::single_component_path_imports)] +#![warn(clippy::macro_use_imports)] + +#[macro_use] +extern crate macro_use_helper as mac; + +#[macro_use] +extern crate clippy_mini_macro_test as mini_mac; + +mod a { + use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro}; + use mac; + use mini_mac::ClippyMiniMacroTest; + use mini_mac; + use mac::{inner::foofoo, inner::try_err}; + use mac::inner; + use mac::inner::nested::string_add; + use mac::inner::nested; + + #[derive(ClippyMiniMacroTest)] + struct Test; + + fn test() { + pub_macro!(); + inner_mod_macro!(); + pub_in_private_macro!(_var); + function_macro!(); + let v: ty_macro!() = Vec::default(); + + inner::try_err!(); + inner::foofoo!(); + nested::string_add!(); + } +} + +fn main() {} diff --git a/tests/ui/macro_use_imports.rs b/tests/ui/macro_use_imports.rs index 52dec0e44b30..7d415222d64f 100644 --- a/tests/ui/macro_use_imports.rs +++ b/tests/ui/macro_use_imports.rs @@ -1,6 +1,7 @@ // compile-flags: --edition 2018 // aux-build:macro_rules.rs // aux-build:macro_use_helper.rs +// run-rustfix #![allow(clippy::single_component_path_imports)] #![warn(clippy::macro_use_imports)] diff --git a/tests/ui/macro_use_imports.stderr b/tests/ui/macro_use_imports.stderr index 83c8ebe6ab9e..6feda8a52226 100644 --- a/tests/ui/macro_use_imports.stderr +++ b/tests/ui/macro_use_imports.stderr @@ -1,28 +1,28 @@ error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:17:5 + --> $DIR/macro_use_imports.rs:18:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;` | = note: `-D clippy::macro-use-imports` implied by `-D warnings` error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:21:5 + --> $DIR/macro_use_imports.rs:20:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};` error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:19:5 + --> $DIR/macro_use_imports.rs:16:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{foofoo::inner, inner::try_err}` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro};` error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:15:5 + --> $DIR/macro_use_imports.rs:22:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro}` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;` error: aborting due to 4 previous errors