diff options
| author | bors <bors@rust-lang.org> | 2023-12-23 13:53:19 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-23 13:53:19 +0000 |
| commit | a24ede2066778f66b5b5e5aa7aa57a6d1be2063a (patch) | |
| tree | 85ef217117334b535768de07cb7207d2bb478c17 | |
| parent | afbb8f31ff0fb66cb7f6ae89606727d01c0a8153 (diff) | |
| parent | 6c9d2ad1d5309a2d99a5ed2bf09bee5ec83e8684 (diff) | |
| download | rust-a24ede2066778f66b5b5e5aa7aa57a6d1be2063a.tar.gz rust-a24ede2066778f66b5b5e5aa7aa57a6d1be2063a.zip | |
Auto merge of #16185 - Young-Flash:fix_auto_remove_brace, r=lnicola
fix: remove wrong comma after remove unnecessary braces  follow up https://github.com/rust-lang/rust-analyzer/pull/16066, close https://github.com/rust-lang/rust-analyzer/issues/16181
| -rw-r--r-- | crates/ide-assists/src/handlers/remove_unused_imports.rs | 105 | ||||
| -rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 7 |
2 files changed, 112 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/remove_unused_imports.rs b/crates/ide-assists/src/handlers/remove_unused_imports.rs index 482c36d502e..859ed1476c4 100644 --- a/crates/ide-assists/src/handlers/remove_unused_imports.rs +++ b/crates/ide-assists/src/handlers/remove_unused_imports.rs @@ -610,6 +610,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] fn remove_nested_all_unused() { check_assist( remove_unused_imports, diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index cfd919ad6f2..a253827495f 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -345,6 +345,12 @@ impl ast::UseTreeList { .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` pub fn remove_unnecessary_braces(mut self) { let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| { @@ -352,6 +358,7 @@ impl ast::UseTreeList { if use_tree_count == 1 { u.l_curly_token().map(ted::remove); u.r_curly_token().map(ted::remove); + u.comma().for_each(ted::remove); } }; |
