Skip to content

Commit 1a55958

Browse files
committed
fix: hide destructure_struct_binding assist if no public fields
1 parent 9f14343 commit 1a55958

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
107107
let visible_fields =
108108
fields.into_iter().filter(|field| field.is_visible_from(ctx.db(), module)).collect_vec();
109109

110+
if visible_fields.is_empty() {
111+
return None;
112+
}
113+
110114
let has_private_members =
111115
(is_non_exhaustive && is_foreign_crate) || visible_fields.len() < n_fields;
112116

@@ -739,4 +743,18 @@ mod tests {
739743
"#,
740744
)
741745
}
746+
747+
#[test]
748+
fn record_struct_no_public_members() {
749+
check_assist_not_applicable(
750+
destructure_struct_binding,
751+
r#"
752+
//- /lib.rs crate:dep
753+
pub struct Foo { bar: i32, baz: i32 };
754+
755+
//- /main.rs crate:main deps:dep
756+
fn main($0foo: dep::Foo) {}
757+
"#,
758+
)
759+
}
742760
}

crates/syntax/src/ast/make.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,10 @@ pub fn record_pat_field_list(
724724
) -> ast::RecordPatFieldList {
725725
let mut fields = fields.into_iter().join(", ");
726726
if let Some(rest_pat) = rest_pat {
727-
format_to!(fields, ", {rest_pat}");
727+
if !fields.is_empty() {
728+
fields.push_str(", ");
729+
}
730+
format_to!(fields, "{rest_pat}");
728731
}
729732
ast_from_text(&format!("fn f(S {{ {fields} }}: ()))"))
730733
}

0 commit comments

Comments
 (0)