Skip to content

Commit a24ede2

Browse files
committed
Auto merge of #16185 - Young-Flash:fix_auto_remove_brace, r=lnicola
fix: remove wrong comma after remove unnecessary braces ![remove_comma](https://github.com/rust-lang/rust-analyzer/assets/71162630/56ef8cfc-a024-4c4a-82de-a8cca513b32e) follow up #16066, close #16181
2 parents afbb8f3 + 6c9d2ad commit a24ede2

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,111 @@ mod b {
609609
);
610610
}
611611

612+
#[test]
613+
fn remove_comma_after_auto_remove_brace() {
614+
check_assist(
615+
remove_unused_imports,
616+
r#"
617+
mod m {
618+
pub mod x {
619+
pub struct A;
620+
pub struct B;
621+
}
622+
pub mod y {
623+
pub struct C;
624+
}
625+
}
626+
627+
$0use m::{
628+
x::{A, B},
629+
y::C,
630+
};$0
631+
632+
fn main() {
633+
B;
634+
}
635+
"#,
636+
r#"
637+
mod m {
638+
pub mod x {
639+
pub struct A;
640+
pub struct B;
641+
}
642+
pub mod y {
643+
pub struct C;
644+
}
645+
}
646+
647+
use m::
648+
x::B
649+
;
650+
651+
fn main() {
652+
B;
653+
}
654+
"#,
655+
);
656+
check_assist(
657+
remove_unused_imports,
658+
r#"
659+
mod m {
660+
pub mod x {
661+
pub struct A;
662+
pub struct B;
663+
}
664+
pub mod y {
665+
pub struct C;
666+
pub struct D;
667+
}
668+
pub mod z {
669+
pub struct E;
670+
pub struct F;
671+
}
672+
}
673+
674+
$0use m::{
675+
x::{A, B},
676+
y::{C, D,},
677+
z::{E, F},
678+
};$0
679+
680+
fn main() {
681+
B;
682+
C;
683+
F;
684+
}
685+
"#,
686+
r#"
687+
mod m {
688+
pub mod x {
689+
pub struct A;
690+
pub struct B;
691+
}
692+
pub mod y {
693+
pub struct C;
694+
pub struct D;
695+
}
696+
pub mod z {
697+
pub struct E;
698+
pub struct F;
699+
}
700+
}
701+
702+
use m::{
703+
x::B,
704+
y::C,
705+
z::F,
706+
};
707+
708+
fn main() {
709+
B;
710+
C;
711+
F;
712+
}
713+
"#,
714+
);
715+
}
716+
612717
#[test]
613718
fn remove_nested_all_unused() {
614719
check_assist(

crates/syntax/src/ast/node_ext.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,20 @@ impl ast::UseTreeList {
345345
.is_some()
346346
}
347347

348+
pub fn comma(&self) -> impl Iterator<Item = SyntaxToken> {
349+
self.syntax()
350+
.children_with_tokens()
351+
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
352+
}
353+
348354
/// Remove the unnecessary braces in current `UseTreeList`
349355
pub fn remove_unnecessary_braces(mut self) {
350356
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
351357
let use_tree_count = u.use_trees().count();
352358
if use_tree_count == 1 {
353359
u.l_curly_token().map(ted::remove);
354360
u.r_curly_token().map(ted::remove);
361+
u.comma().for_each(ted::remove);
355362
}
356363
};
357364

0 commit comments

Comments
 (0)