Skip to content

Commit 65ed198

Browse files
committed
Auto merge of rust-lang#16066 - Young-Flash:auto_remove_brace, r=lnicola
fix: auto remove unnecessary braces after remove unused imports before ![before](https://github.com/rust-lang/rust-analyzer/assets/71162630/8d44f955-f84f-4a92-b13f-5a2dee2ded36) after ![after](https://github.com/rust-lang/rust-analyzer/assets/71162630/1eab23c8-39bd-4711-97c1-d483ce400a18)
2 parents 7bdf48c + 4cd939a commit 65ed198

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ mod z {
423423
struct X();
424424
struct Y();
425425
mod z {
426-
use super::{X};
426+
use super::X;
427427
428428
fn w() {
429429
let x = X();
@@ -495,7 +495,7 @@ struct X();
495495
mod y {
496496
struct Y();
497497
mod z {
498-
use crate::{X};
498+
use crate::X;
499499
fn f() {
500500
let x = X();
501501
}
@@ -526,7 +526,7 @@ struct X();
526526
mod y {
527527
struct Y();
528528
mod z {
529-
use crate::{y::Y};
529+
use crate::y::Y;
530530
fn f() {
531531
let y = Y();
532532
}
@@ -536,6 +536,79 @@ mod y {
536536
);
537537
}
538538

539+
#[test]
540+
fn remove_unused_auto_remove_brace_nested() {
541+
check_assist(
542+
remove_unused_imports,
543+
r#"
544+
mod a {
545+
pub struct A();
546+
}
547+
mod b {
548+
struct F();
549+
mod c {
550+
$0use {{super::{{
551+
{d::{{{{{{{S, U}}}}}}}},
552+
{{{{e::{H, L, {{{R}}}}}}}},
553+
F, super::a::A
554+
}}}};$0
555+
fn f() {
556+
let f = F();
557+
let l = L();
558+
let a = A();
559+
let s = S();
560+
let h = H();
561+
}
562+
}
563+
564+
mod d {
565+
pub struct S();
566+
pub struct U();
567+
}
568+
569+
mod e {
570+
pub struct H();
571+
pub struct L();
572+
pub struct R();
573+
}
574+
}
575+
"#,
576+
r#"
577+
mod a {
578+
pub struct A();
579+
}
580+
mod b {
581+
struct F();
582+
mod c {
583+
use super::{
584+
d::S,
585+
e::{H, L},
586+
F, super::a::A
587+
};
588+
fn f() {
589+
let f = F();
590+
let l = L();
591+
let a = A();
592+
let s = S();
593+
let h = H();
594+
}
595+
}
596+
597+
mod d {
598+
pub struct S();
599+
pub struct U();
600+
}
601+
602+
mod e {
603+
pub struct H();
604+
pub struct L();
605+
pub struct R();
606+
}
607+
}
608+
"#,
609+
);
610+
}
611+
539612
#[test]
540613
fn remove_nested_all_unused() {
541614
check_assist(

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl ast::UseTree {
414414
u.remove_recursive();
415415
}
416416
}
417+
u.remove_unnecessary_braces();
417418
}
418419
}
419420

crates/syntax/src/ast/node_ext.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rowan::{GreenNodeData, GreenTokenData};
1111

1212
use crate::{
1313
ast::{self, support, AstNode, AstToken, HasAttrs, HasGenericParams, HasName, SyntaxNode},
14-
NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
14+
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
1515
};
1616

1717
impl ast::Lifetime {
@@ -323,6 +323,10 @@ impl ast::UseTree {
323323
pub fn is_simple_path(&self) -> bool {
324324
self.use_tree_list().is_none() && self.star_token().is_none()
325325
}
326+
327+
pub fn parent_use_tree_list(&self) -> Option<ast::UseTreeList> {
328+
self.syntax().parent().and_then(ast::UseTreeList::cast)
329+
}
326330
}
327331

328332
impl ast::UseTreeList {
@@ -340,6 +344,27 @@ impl ast::UseTreeList {
340344
.find_map(ast::Comment::cast)
341345
.is_some()
342346
}
347+
348+
/// Remove the unnecessary braces in current `UseTreeList`
349+
pub fn remove_unnecessary_braces(mut self) {
350+
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
351+
let use_tree_count = u.use_trees().count();
352+
if use_tree_count == 1 {
353+
u.l_curly_token().map(ted::remove);
354+
u.r_curly_token().map(ted::remove);
355+
}
356+
};
357+
358+
// take `use crate::{{{{A}}}}` for example
359+
// the below remove the innermost {}, got `use crate::{{{A}}}`
360+
remove_brace_in_use_tree_list(&self);
361+
362+
// the below remove othe unnecessary {}, got `use crate::A`
363+
while let Some(parent_use_tree_list) = self.parent_use_tree().parent_use_tree_list() {
364+
remove_brace_in_use_tree_list(&parent_use_tree_list);
365+
self = parent_use_tree_list;
366+
}
367+
}
343368
}
344369

345370
impl ast::Impl {

0 commit comments

Comments
 (0)