Skip to content

Commit

Permalink
fix: hide destructure_struct_binding assist if no public fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindronics committed Mar 4, 2024
1 parent 9f14343 commit 1a55958
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions crates/ide-assists/src/handlers/destructure_struct_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -739,4 +743,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) {}
"#,
)
}
}
5 changes: 4 additions & 1 deletion crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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} }}: ()))"))
}
Expand Down

0 comments on commit 1a55958

Please sign in to comment.