Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #16752 - Lindronics:destructure-struct-binding, r=Veykril
fix: Don't destructure struct with no public fields Unfortunately I missed this case in #16638. If a struct only has private members, the assist should not be applicable. Though valid syntax exists (`Foo { .. }`), it isn't particularly useful. Since this case applies to a lot of common types (`Arc`, `Vec`, ...), it probably makes the most sense to hide the action. As a side effect, this also disables the action for unit structs, where it also isn't particularly useful. I'd be open to changing it though if you think it makes more sense to keep it. This also fixes the `make::record_pat_field_list` function to produce valid syntax if the field list is empty, as it is used in other places too. ## Current behaviour ```rust // In crate `other_crate` pub struct Foo { bar: i32 } // In current crate fn do_something(foo: other_crate::Foo) {} // Becomes fn do_something(other_crate::Foo { , .. }: other_crate::Foo) {} ``` ## Fixed behaviour Assist should not be applicable in this case anymore.
bors 2024-03-05
parent 58e10c7 · parent 2a4ba42 · commit 0c2e9fe
-rw-r--r--crates/ide-assists/src/handlers/destructure_struct_binding.rs27
-rw-r--r--crates/syntax/src/ast/make.rs5
2 files changed, 23 insertions, 9 deletions
diff --git a/crates/ide-assists/src/handlers/destructure_struct_binding.rs b/crates/ide-assists/src/handlers/destructure_struct_binding.rs
index 4edc52b614..c1a3f93026 100644
--- a/crates/ide-assists/src/handlers/destructure_struct_binding.rs
+++ b/crates/ide-assists/src/handlers/destructure_struct_binding.rs
@@ -107,6 +107,10 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
let visible_fields =
fields.into_iter().filter(|field| field.is_visible_from(ctx.db(), module)).collect_vec();
+ if visible_fields.is_empty() {
+ return None;
+ }
+
let has_private_members =
(is_non_exhaustive && is_foreign_crate) || visible_fields.len() < n_fields;
@@ -413,7 +417,7 @@ mod tests {
#[test]
fn unit_struct() {
- check_assist(
+ check_assist_not_applicable(
destructure_struct_binding,
r#"
struct Foo;
@@ -422,13 +426,6 @@ mod tests {
let $0foo = Foo;
}
"#,
- r#"
- struct Foo;
-
- fn main() {
- let Foo = Foo;
- }
- "#,
)
}
@@ -739,4 +736,18 @@ mod tests {
"#,
)
}
+
+ #[test]
+ fn record_struct_no_public_members() {
+ check_assist_not_applicable(
+ destructure_struct_binding,
+ r#"
+ //- /lib.rs crate:dep
+ pub struct Foo { bar: i32, baz: i32 };
+
+ //- /main.rs crate:main deps:dep
+ fn main($0foo: dep::Foo) {}
+ "#,
+ )
+ }
}
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index f299dda4f0..ff18fee9ba 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -724,7 +724,10 @@ pub fn record_pat_field_list(
) -> ast::RecordPatFieldList {
let mut fields = fields.into_iter().join(", ");
if let Some(rest_pat) = rest_pat {
- format_to!(fields, ", {rest_pat}");
+ if !fields.is_empty() {
+ fields.push_str(", ");
+ }
+ format_to!(fields, "{rest_pat}");
}
ast_from_text(&format!("fn f(S {{ {fields} }}: ()))"))
}