Skip to content

Commit 415e357

Browse files
harrysarsonharrysarson-signaloid
authored andcommitted
braces around {self} in UseTree are not unnecessary
Before this commit `UseTree::remove_unnecessary_braces` removed the braces around `{self}` in `use x::y::{self};` but `use x::y::self;` is not valid rust.
1 parent 47a901b commit 415e357

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

crates/ide-assists/src/handlers/remove_unused_imports.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,40 @@ mod z {
776776
);
777777
}
778778

779+
#[test]
780+
fn remove_unused_fixes_nested_self() {
781+
check_assist(
782+
remove_unused_imports,
783+
r#"
784+
mod inner {
785+
pub struct X();
786+
pub struct Y();
787+
}
788+
789+
mod z {
790+
use super::inner::{self, X}$0;
791+
792+
fn f() {
793+
let y = inner::Y();
794+
}
795+
}
796+
"#,
797+
r#"mod inner {
798+
pub struct X();
799+
pub struct Y();
800+
}
801+
802+
mod z {
803+
use super::inner::{self};
804+
805+
fn f() {
806+
let y = inner::Y();
807+
}
808+
}
809+
"#,
810+
);
811+
}
812+
779813
#[test]
780814
fn dont_remove_used_glob() {
781815
check_assist_not_applicable(

crates/syntax/src/ast/node_ext.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,24 @@ impl ast::UseTreeList {
376376
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
377377
}
378378

379+
pub fn is_just_self(&self) -> bool {
380+
if let Some((single_subtree,)) = self.use_trees().collect_tuple() {
381+
fn path_is_self(path: &ast::Path) -> bool {
382+
path.segment().and_then(|seg| seg.self_token()).is_some()
383+
&& path.qualifier().is_none()
384+
}
385+
386+
single_subtree.path().as_ref().map_or(false, path_is_self)
387+
} else {
388+
false
389+
}
390+
}
391+
379392
/// Remove the unnecessary braces in current `UseTreeList`
380393
pub fn remove_unnecessary_braces(mut self) {
381394
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
382395
let use_tree_count = u.use_trees().count();
383-
if use_tree_count == 1 {
396+
if use_tree_count == 1 && !u.is_just_self() {
384397
if let Some(a) = u.l_curly_token() {
385398
ted::remove(a)
386399
}

0 commit comments

Comments
 (0)