Skip to content

Commit

Permalink
Fix suggestion output, add run-rustfix to test file, stop sorting imp…
Browse files Browse the repository at this point in the history
…ort segments duh
  • Loading branch information
DevinR528 committed Jun 8, 2020
1 parent 8c5a5a9 commit 288df59
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
35 changes: 18 additions & 17 deletions clippy_lints/src/macro_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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));
}
},
}
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions tests/ui/macro_use_imports.fixed
Original file line number Diff line number Diff line change
@@ -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() {}
1 change: 1 addition & 0 deletions tests/ui/macro_use_imports.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/macro_use_imports.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 288df59

Please sign in to comment.