Skip to content

Commit 6d5bfae

Browse files
authored
Merge pull request #20709 from A4-Tacks/destruct-panic-on-not-add-deref-and-paren
Fix panic `!self.data().mutable` for destructure_struct_binding
2 parents da47c3d + f34e9ca commit 6d5bfae

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn build_usage_edit(
295295
Some(field_expr) => Some({
296296
let field_name: SmolStr = field_expr.name_ref()?.to_string().into();
297297
let new_field_name = field_names.get(&field_name)?;
298-
let new_expr = make.expr_path(ast::make::ext::ident_path(new_field_name));
298+
let new_expr = ast::make::expr_path(ast::make::ext::ident_path(new_field_name));
299299

300300
// If struct binding is a reference, we might need to deref field usages
301301
if data.is_ref {
@@ -305,7 +305,7 @@ fn build_usage_edit(
305305
ref_data.wrap_expr(new_expr).syntax().clone_for_update(),
306306
)
307307
} else {
308-
(field_expr.syntax().clone(), new_expr.syntax().clone())
308+
(field_expr.syntax().clone(), new_expr.syntax().clone_for_update())
309309
}
310310
}),
311311
None => Some((
@@ -697,6 +697,52 @@ mod tests {
697697
)
698698
}
699699

700+
#[test]
701+
fn ref_not_add_parenthesis_and_deref_record() {
702+
check_assist(
703+
destructure_struct_binding,
704+
r#"
705+
struct Foo { bar: i32, baz: i32 }
706+
707+
fn main() {
708+
let $0foo = &Foo { bar: 1, baz: 2 };
709+
let _ = &foo.bar;
710+
}
711+
"#,
712+
r#"
713+
struct Foo { bar: i32, baz: i32 }
714+
715+
fn main() {
716+
let Foo { bar, baz } = &Foo { bar: 1, baz: 2 };
717+
let _ = bar;
718+
}
719+
"#,
720+
)
721+
}
722+
723+
#[test]
724+
fn ref_not_add_parenthesis_and_deref_tuple() {
725+
check_assist(
726+
destructure_struct_binding,
727+
r#"
728+
struct Foo(i32, i32);
729+
730+
fn main() {
731+
let $0foo = &Foo(1, 2);
732+
let _ = &foo.0;
733+
}
734+
"#,
735+
r#"
736+
struct Foo(i32, i32);
737+
738+
fn main() {
739+
let Foo(_0, _1) = &Foo(1, 2);
740+
let _ = _0;
741+
}
742+
"#,
743+
)
744+
}
745+
700746
#[test]
701747
fn record_struct_name_collision() {
702748
check_assist(

0 commit comments

Comments
 (0)