diff options
| author | Shoyu Vanilla (Flint) <modulo641@gmail.com> | 2025-09-20 12:41:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-20 12:41:21 +0000 |
| commit | a08e8ba4ff2ea1e146c99d221d16c883ba171ead (patch) | |
| tree | 9aca29c1dc02f712b7fcb79b76d8a734e0a457a9 /src | |
| parent | d0da6a12d92f00fbaf30326e012b907b7555770a (diff) | |
| parent | 8eda71b98a8540f848cfd33b58f6d284edb64073 (diff) | |
| download | rust-a08e8ba4ff2ea1e146c99d221d16c883ba171ead.tar.gz rust-a08e8ba4ff2ea1e146c99d221d16c883ba171ead.zip | |
Merge pull request #20709 from A4-Tacks/destruct-panic-on-not-add-deref-and-paren
Fix panic `!self.data().mutable` for destructure_struct_binding
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs index 63a41ae5dc8..27755db93c8 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs @@ -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(( @@ -698,6 +698,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( destructure_struct_binding, |
