about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-29 14:07:50 +0000
committerbors <bors@rust-lang.org>2024-02-29 14:07:50 +0000
commit71eb5400a3a527d25b1cd8a22f3ce3f45813576d (patch)
treebb3dbac0aff82e9704f1806bbc2d09b09d94690c /editors/code
parent0ec6015b6ebe6c7691d9f6c0cd4d0e1679b336ba (diff)
parentdc7b502689fcc8365ace88c96a439c4825dae459 (diff)
downloadrust-71eb5400a3a527d25b1cd8a22f3ce3f45813576d.tar.gz
rust-71eb5400a3a527d25b1cd8a22f3ce3f45813576d.zip
Auto merge of #16638 - Lindronics:destructure-struct-binding, r=Veykril
feature: Add `destructure_struct_binding`

Adds an assist for destructuring a struct in a binding (#8673). I saw that #13997 has been abandoned for a while, so I thought I'd give it a go.

## Example

```rust
let foo = Foo { bar: 1, baz: 2 };
let bar2 = foo.bar;
let baz2 = foo.baz;
let foo2 = foo;

let fizz = Fizz(1, 2);
let buzz = fizz.0;
```
becomes
```rust
let Foo { bar, baz } = Foo { bar: 1, baz: 2 };
let bar2 = bar;
let baz2 = baz;
let foo2 = todo!();

let Fizz(_0, _1) = Fizz(1, 2);
let buzz = _0;
```

More examples in the tests.

## What is included?

- [x] Destructure record, tuple, and unit struct bindings
- [x] Edit field usages
- [x] Non-exhaustive structs in foreign crates and private fields get hidden behind `..`
- [x] Nested bindings
- [x] Carry over `mut` and `ref mut` in nested bindings to fields, i.e. `let Foo { ref mut bar } = ...` becomes `let Foo { bar: Bar { baz: ref mut baz } } = ...`
- [x] Attempt to resolve collisions with other names in the scope
- [x] If the binding is to a reference, field usages are dereferenced if required
- [x] Use shorthand notation if possible

## Known limitations

- `let foo = Foo { bar: 1 }; foo;` currently results in `let Foo { bar } = Foo { bar: 1 }; todo!();` instead of reassembling the struct. This requires user intervention.
- Unused fields are not currently omitted. I thought that this is more ergonomic, as there already is a quick fix action for adding `: _` to unused field patterns.
Diffstat (limited to 'editors/code')
0 files changed, 0 insertions, 0 deletions