Skip to content

Commit 856a181

Browse files
committed
Fix autofix for self and self as … in unused_imports lint
This fixes two problems with the autofixes for the `unused_imports` lint: - `use std::collections::{HashMap, self as coll};` would suggest, when `HashMap` is unused, the incorrect `use std::collections::self as coll;` which does not compile. - `use std::borrow::{self, Cow};` would suggest, when `self` is unused, `use std::borrow::{Cow};`, which contains unnecessary brackets.
1 parent ae8ab87 commit 856a181

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

compiler/rustc_resolve/src/check_unused.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ fn calc_unused_spans(
337337
}
338338
}
339339
contains_self |= use_tree.prefix == kw::SelfLower
340-
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(None));
340+
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(_))
341+
&& !unused_import.unused.contains(&use_tree_id);
341342
previous_unused = remove.is_some();
342343
}
343344
if unused_spans.is_empty() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_imports)]
4+
#![allow(unreachable_code)]
5+
6+
use std::collections::{self as coll};
7+
//~^ ERROR unused import: `HashMap`
8+
9+
//~^ ERROR unused import: `self as std_io`
10+
11+
use std::sync::Mutex;
12+
//~^ ERROR unused import: `self as std_sync`
13+
14+
use std::sync::mpsc::Sender;
15+
//~^ ERROR unused import: `self as std_sync_mpsc`
16+
17+
use std::collections::hash_map::{self as std_coll_hm};
18+
//~^ ERROR unused import: `Keys`
19+
20+
use std::borrow::Cow;
21+
//~^ ERROR unused import: `self`
22+
23+
fn main() {
24+
let _ = coll::BTreeSet::<String>::default();
25+
let _ = Mutex::new(String::new());
26+
let _: Cow<'static, str> = "foo".into();
27+
let _: Sender<u32> = todo!();
28+
let _: std_coll_hm::Entry<'static, u32, u32> = todo!();
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_imports)]
4+
#![allow(unreachable_code)]
5+
6+
use std::collections::{HashMap, self as coll};
7+
//~^ ERROR unused import: `HashMap`
8+
9+
use std::io::{self as std_io};
10+
//~^ ERROR unused import: `self as std_io`
11+
12+
use std::sync::{Mutex, self as std_sync};
13+
//~^ ERROR unused import: `self as std_sync`
14+
15+
use std::sync::{mpsc::{self as std_sync_mpsc, Sender}};
16+
//~^ ERROR unused import: `self as std_sync_mpsc`
17+
18+
use std::collections::{hash_map::{self as std_coll_hm, Keys}};
19+
//~^ ERROR unused import: `Keys`
20+
21+
use std::borrow::{self, Cow};
22+
//~^ ERROR unused import: `self`
23+
24+
fn main() {
25+
let _ = coll::BTreeSet::<String>::default();
26+
let _ = Mutex::new(String::new());
27+
let _: Cow<'static, str> = "foo".into();
28+
let _: Sender<u32> = todo!();
29+
let _: std_coll_hm::Entry<'static, u32, u32> = todo!();
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: unused import: `HashMap`
2+
--> $DIR/lint-unused-imports-self-single.rs:6:24
3+
|
4+
LL | use std::collections::{HashMap, self as coll};
5+
| ^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-unused-imports-self-single.rs:3:9
9+
|
10+
LL | #![deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: unused import: `self as std_io`
14+
--> $DIR/lint-unused-imports-self-single.rs:9:15
15+
|
16+
LL | use std::io::{self as std_io};
17+
| ^^^^^^^^^^^^^^
18+
19+
error: unused import: `self as std_sync`
20+
--> $DIR/lint-unused-imports-self-single.rs:12:24
21+
|
22+
LL | use std::sync::{Mutex, self as std_sync};
23+
| ^^^^^^^^^^^^^^^^
24+
25+
error: unused import: `self as std_sync_mpsc`
26+
--> $DIR/lint-unused-imports-self-single.rs:15:24
27+
|
28+
LL | use std::sync::{mpsc::{self as std_sync_mpsc, Sender}};
29+
| ^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: unused import: `Keys`
32+
--> $DIR/lint-unused-imports-self-single.rs:18:56
33+
|
34+
LL | use std::collections::{hash_map::{self as std_coll_hm, Keys}};
35+
| ^^^^
36+
37+
error: unused import: `self`
38+
--> $DIR/lint-unused-imports-self-single.rs:21:19
39+
|
40+
LL | use std::borrow::{self, Cow};
41+
| ^^^^
42+
43+
error: aborting due to 6 previous errors
44+

0 commit comments

Comments
 (0)