Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autofix for self and self as … in unused_imports lint #138886

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
@@ -337,7 +337,8 @@ fn calc_unused_spans(
}
}
contains_self |= use_tree.prefix == kw::SelfLower
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(None));
&& matches!(use_tree.kind, ast::UseTreeKind::Simple(_))
&& !unused_import.unused.contains(&use_tree_id);
previous_unused = remove.is_some();
}
if unused_spans.is_empty() {
29 changes: 29 additions & 0 deletions tests/ui/lint/unused/lint-unused-imports-self-single.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-rustfix

#![deny(unused_imports)]
#![allow(unreachable_code)]

use std::collections::{self as coll};
//~^ ERROR unused import: `HashMap`

//~^ ERROR unused import: `self as std_io`

use std::sync::Mutex;
//~^ ERROR unused import: `self as std_sync`

use std::sync::mpsc::Sender;
//~^ ERROR unused import: `self as std_sync_mpsc`

use std::collections::hash_map::{self as std_coll_hm};
//~^ ERROR unused import: `Keys`

use std::borrow::Cow;
//~^ ERROR unused import: `self`

fn main() {
let _ = coll::BTreeSet::<String>::default();
let _ = Mutex::new(String::new());
let _: Cow<'static, str> = "foo".into();
let _: Sender<u32> = todo!();
let _: std_coll_hm::Entry<'static, u32, u32> = todo!();
}
30 changes: 30 additions & 0 deletions tests/ui/lint/unused/lint-unused-imports-self-single.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//@ run-rustfix

#![deny(unused_imports)]
#![allow(unreachable_code)]

use std::collections::{HashMap, self as coll};
//~^ ERROR unused import: `HashMap`

use std::io::{self as std_io};
//~^ ERROR unused import: `self as std_io`

use std::sync::{Mutex, self as std_sync};
//~^ ERROR unused import: `self as std_sync`

use std::sync::{mpsc::{self as std_sync_mpsc, Sender}};
//~^ ERROR unused import: `self as std_sync_mpsc`

use std::collections::{hash_map::{self as std_coll_hm, Keys}};
//~^ ERROR unused import: `Keys`

use std::borrow::{self, Cow};
//~^ ERROR unused import: `self`

fn main() {
let _ = coll::BTreeSet::<String>::default();
let _ = Mutex::new(String::new());
let _: Cow<'static, str> = "foo".into();
let _: Sender<u32> = todo!();
let _: std_coll_hm::Entry<'static, u32, u32> = todo!();
}
44 changes: 44 additions & 0 deletions tests/ui/lint/unused/lint-unused-imports-self-single.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error: unused import: `HashMap`
--> $DIR/lint-unused-imports-self-single.rs:6:24
|
LL | use std::collections::{HashMap, self as coll};
| ^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-unused-imports-self-single.rs:3:9
|
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unused import: `self as std_io`
--> $DIR/lint-unused-imports-self-single.rs:9:15
|
LL | use std::io::{self as std_io};
| ^^^^^^^^^^^^^^

error: unused import: `self as std_sync`
--> $DIR/lint-unused-imports-self-single.rs:12:24
|
LL | use std::sync::{Mutex, self as std_sync};
| ^^^^^^^^^^^^^^^^

error: unused import: `self as std_sync_mpsc`
--> $DIR/lint-unused-imports-self-single.rs:15:24
|
LL | use std::sync::{mpsc::{self as std_sync_mpsc, Sender}};
| ^^^^^^^^^^^^^^^^^^^^^

error: unused import: `Keys`
--> $DIR/lint-unused-imports-self-single.rs:18:56
|
LL | use std::collections::{hash_map::{self as std_coll_hm, Keys}};
| ^^^^

error: unused import: `self`
--> $DIR/lint-unused-imports-self-single.rs:21:19
|
LL | use std::borrow::{self, Cow};
| ^^^^

error: aborting due to 6 previous errors

Loading
Oops, something went wrong.