Skip to content

Commit

Permalink
braces around {self} in UseTree are not unnecessary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harrysarson authored and harrysarson-signaloid committed Apr 25, 2024
1 parent 47a901b commit 415e357
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
34 changes: 34 additions & 0 deletions crates/ide-assists/src/handlers/remove_unused_imports.rs
Expand Up @@ -776,6 +776,40 @@ mod z {
);
}

#[test]
fn remove_unused_fixes_nested_self() {
check_assist(
remove_unused_imports,
r#"
mod inner {
pub struct X();
pub struct Y();
}
mod z {
use super::inner::{self, X}$0;
fn f() {
let y = inner::Y();
}
}
"#,
r#"mod inner {
pub struct X();
pub struct Y();
}
mod z {
use super::inner::{self};
fn f() {
let y = inner::Y();
}
}
"#,
);
}

#[test]
fn dont_remove_used_glob() {
check_assist_not_applicable(
Expand Down
15 changes: 14 additions & 1 deletion crates/syntax/src/ast/node_ext.rs
Expand Up @@ -376,11 +376,24 @@ impl ast::UseTreeList {
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
}

pub fn is_just_self(&self) -> bool {
if let Some((single_subtree,)) = self.use_trees().collect_tuple() {
fn path_is_self(path: &ast::Path) -> bool {
path.segment().and_then(|seg| seg.self_token()).is_some()
&& path.qualifier().is_none()
}

single_subtree.path().as_ref().map_or(false, path_is_self)
} else {
false
}
}

/// Remove the unnecessary braces in current `UseTreeList`
pub fn remove_unnecessary_braces(mut self) {
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
let use_tree_count = u.use_trees().count();
if use_tree_count == 1 {
if use_tree_count == 1 && !u.is_just_self() {
if let Some(a) = u.l_curly_token() {
ted::remove(a)
}
Expand Down

0 comments on commit 415e357

Please sign in to comment.