Merge pull request #20709 from A4-Tacks/destruct-panic-on-not-add-deref-and-paren

Fix panic `!self.data().mutable` for destructure_struct_binding
This commit is contained in:
Shoyu Vanilla (Flint) 2025-09-20 12:41:21 +00:00 committed by GitHub
commit 6d5bfaee6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -295,7 +295,7 @@ fn build_usage_edit(
Some(field_expr) => Some({
let field_name: SmolStr = field_expr.name_ref()?.to_string().into();
let new_field_name = field_names.get(&field_name)?;
let new_expr = make.expr_path(ast::make::ext::ident_path(new_field_name));
let new_expr = ast::make::expr_path(ast::make::ext::ident_path(new_field_name));
// If struct binding is a reference, we might need to deref field usages
if data.is_ref {
@ -305,7 +305,7 @@ fn build_usage_edit(
ref_data.wrap_expr(new_expr).syntax().clone_for_update(),
)
} else {
(field_expr.syntax().clone(), new_expr.syntax().clone())
(field_expr.syntax().clone(), new_expr.syntax().clone_for_update())
}
}),
None => Some((
@ -697,6 +697,52 @@ mod tests {
)
}
#[test]
fn ref_not_add_parenthesis_and_deref_record() {
check_assist(
destructure_struct_binding,
r#"
struct Foo { bar: i32, baz: i32 }
fn main() {
let $0foo = &Foo { bar: 1, baz: 2 };
let _ = &foo.bar;
}
"#,
r#"
struct Foo { bar: i32, baz: i32 }
fn main() {
let Foo { bar, baz } = &Foo { bar: 1, baz: 2 };
let _ = bar;
}
"#,
)
}
#[test]
fn ref_not_add_parenthesis_and_deref_tuple() {
check_assist(
destructure_struct_binding,
r#"
struct Foo(i32, i32);
fn main() {
let $0foo = &Foo(1, 2);
let _ = &foo.0;
}
"#,
r#"
struct Foo(i32, i32);
fn main() {
let Foo(_0, _1) = &Foo(1, 2);
let _ = _0;
}
"#,
)
}
#[test]
fn record_struct_name_collision() {
check_assist(