Skip to content

Commit 288df59

Browse files
committed
Fix suggestion output, add run-rustfix to test file, stop sorting import segments duh
1 parent 8c5a5a9 commit 288df59

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

clippy_lints/src/macro_use.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -167,32 +167,33 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
167167
[] => unreachable!("this should never be empty"),
168168
[_] => unreachable!("path must have two segments ?"),
169169
[root, item] => {
170-
if !check_dup.contains(&item.to_string()) {
170+
if !check_dup.contains(&(*item).to_string()) {
171171
used.entry((root.to_string(), span))
172-
.or_insert(vec![])
172+
.or_insert_with(|| vec![])
173173
.push(item.to_string());
174174
check_dup.push(item.to_string());
175175
}
176176
},
177177
[root, rest @ ..] => {
178-
if !rest.iter().all(|item| !check_dup.contains(&item.to_string())) {
179-
let mut rest = rest.to_vec();
180-
rest.sort();
181-
used.entry((root.to_string(), span))
182-
.or_insert(vec![])
183-
.push(rest.join("::"));
184-
check_dup.extend(rest.iter().map(ToString::to_string));
185-
} else {
186-
let mut filtered = rest
178+
if rest.iter().all(|item| !check_dup.contains(&(*item).to_string())) {
179+
let filtered = rest
187180
.iter()
188-
.filter(|item| !check_dup.contains(&item.to_string()))
189-
.map(ToString::to_string)
181+
.filter_map(|item| if check_dup.contains(&(*item).to_string()) {
182+
None
183+
} else {
184+
Some(item.to_string())
185+
})
190186
.collect::<Vec<_>>();
191-
filtered.sort();
192-
used.entry((root.to_string(), span))
193-
.or_insert(vec![])
187+
used.entry(((*root).to_string(), span))
188+
.or_insert_with(|| vec![])
194189
.push(filtered.join("::"));
195190
check_dup.extend(filtered);
191+
} else {
192+
let rest = rest.to_vec();
193+
used.entry((root.to_string(), span))
194+
.or_insert_with(|| vec![])
195+
.push(rest.join("::"));
196+
check_dup.extend(rest.iter().map(ToString::to_string));
196197
}
197198
},
198199
}
@@ -212,7 +213,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
212213
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
213214
if self.mac_refs.is_empty() {
214215
for (span, import) in suggestions {
215-
let help = format!("use {}", import);
216+
let help = format!("use {};", import);
216217
span_lint_and_sugg(
217218
cx,
218219
MACRO_USE_IMPORTS,

tests/ui/macro_use_imports.fixed

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// compile-flags: --edition 2018
2+
// aux-build:macro_rules.rs
3+
// aux-build:macro_use_helper.rs
4+
// run-rustfix
5+
6+
#![allow(clippy::single_component_path_imports)]
7+
#![warn(clippy::macro_use_imports)]
8+
9+
#[macro_use]
10+
extern crate macro_use_helper as mac;
11+
12+
#[macro_use]
13+
extern crate clippy_mini_macro_test as mini_mac;
14+
15+
mod a {
16+
use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro};
17+
use mac;
18+
use mini_mac::ClippyMiniMacroTest;
19+
use mini_mac;
20+
use mac::{inner::foofoo, inner::try_err};
21+
use mac::inner;
22+
use mac::inner::nested::string_add;
23+
use mac::inner::nested;
24+
25+
#[derive(ClippyMiniMacroTest)]
26+
struct Test;
27+
28+
fn test() {
29+
pub_macro!();
30+
inner_mod_macro!();
31+
pub_in_private_macro!(_var);
32+
function_macro!();
33+
let v: ty_macro!() = Vec::default();
34+
35+
inner::try_err!();
36+
inner::foofoo!();
37+
nested::string_add!();
38+
}
39+
}
40+
41+
fn main() {}

tests/ui/macro_use_imports.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: --edition 2018
22
// aux-build:macro_rules.rs
33
// aux-build:macro_use_helper.rs
4+
// run-rustfix
45

56
#![allow(clippy::single_component_path_imports)]
67
#![warn(clippy::macro_use_imports)]

tests/ui/macro_use_imports.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2-
--> $DIR/macro_use_imports.rs:17:5
2+
--> $DIR/macro_use_imports.rs:18:5
33
|
44
LL | #[macro_use]
5-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest`
5+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
66
|
77
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
88

99
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
10-
--> $DIR/macro_use_imports.rs:21:5
10+
--> $DIR/macro_use_imports.rs:20:5
1111
|
1212
LL | #[macro_use]
13-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add`
13+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};`
1414

1515
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
16-
--> $DIR/macro_use_imports.rs:19:5
16+
--> $DIR/macro_use_imports.rs:16:5
1717
|
1818
LL | #[macro_use]
19-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{foofoo::inner, inner::try_err}`
19+
| ^^^^^^^^^^^^ 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};`
2020

2121
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
22-
--> $DIR/macro_use_imports.rs:15:5
22+
--> $DIR/macro_use_imports.rs:22:5
2323
|
2424
LL | #[macro_use]
25-
| ^^^^^^^^^^^^ 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}`
25+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
2626

2727
error: aborting due to 4 previous errors
2828

0 commit comments

Comments
 (0)