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 https://github.com/rust-lang/rust-analyzer/pull/16066, close https://github.com/rust-lang/rust-analyzer/issues/16181
This commit is contained in:
bors 2023-12-23 13:53:19 +00:00
commit a24ede2066
2 changed files with 112 additions and 0 deletions

View File

@ -609,6 +609,111 @@ mod b {
); );
} }
#[test]
fn remove_comma_after_auto_remove_brace() {
check_assist(
remove_unused_imports,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
}
}
$0use m::{
x::{A, B},
y::C,
};$0
fn main() {
B;
}
"#,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
}
}
use m::
x::B
;
fn main() {
B;
}
"#,
);
check_assist(
remove_unused_imports,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
pub struct D;
}
pub mod z {
pub struct E;
pub struct F;
}
}
$0use m::{
x::{A, B},
y::{C, D,},
z::{E, F},
};$0
fn main() {
B;
C;
F;
}
"#,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
pub struct D;
}
pub mod z {
pub struct E;
pub struct F;
}
}
use m::{
x::B,
y::C,
z::F,
};
fn main() {
B;
C;
F;
}
"#,
);
}
#[test] #[test]
fn remove_nested_all_unused() { fn remove_nested_all_unused() {
check_assist( check_assist(

View File

@ -345,6 +345,12 @@ impl ast::UseTreeList {
.is_some() .is_some()
} }
pub fn comma(&self) -> impl Iterator<Item = SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
}
/// Remove the unnecessary braces in current `UseTreeList` /// Remove the unnecessary braces in current `UseTreeList`
pub fn remove_unnecessary_braces(mut self) { pub fn remove_unnecessary_braces(mut self) {
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| { let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
@ -352,6 +358,7 @@ impl ast::UseTreeList {
if use_tree_count == 1 { if use_tree_count == 1 {
u.l_curly_token().map(ted::remove); u.l_curly_token().map(ted::remove);
u.r_curly_token().map(ted::remove); u.r_curly_token().map(ted::remove);
u.comma().for_each(ted::remove);
} }
}; };