rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Add ide-assists: merge let statements

Open A4-Tacks opened this issue 7 months ago • 3 comments

Close rust-lang/rust-analyzer#10651

Assist: merge_let_stmts

Merge multiple lets into a let tuple pattern, allowing for the use of multiple identical let-else.

fn main() {
    $0let a = 2;
    let b = 3;$0
}

->

fn main() {
    let (a, b) = (2, 3);
}

A4-Tacks avatar May 11 '25 08:05 A4-Tacks

:warning: Warning :warning:

  • There are issue links (such as #123) in the commit messages of the following commits. Please remove them as they will spam the issue with references to the commit.
    • 794320f6ec38b2b0c23d4a967f01aa6e040d1b6e

rustbot avatar May 11 '25 08:05 rustbot

Thinking about this a bit more, I really don't see the usefulness in this. I am not quite sure why you'd want to turn multiple let statements into a single one that just does destructuring on the left, and merging via a tuple expression on the right. That seems like a net readability downgrade to me.

Veykril avatar Jun 02 '25 11:06 Veykril

For related simple assignments, in single let statement may be more readable

Such as let (x, y) = (10, 5);

A4-Tacks avatar Jun 02 '25 22:06 A4-Tacks

r? @rust-lang/rust-analyzer, as per my comment above https://github.com/rust-lang/rust-analyzer/pull/19777#issuecomment-2930224427 I am inclined to close this, thoughts?

Veykril avatar Jul 04 '25 09:07 Veykril

I don't know. This could be a useful step in a larger refactoring. But I'm not sure if it's worth having for that.

Random thought, I could imagine our "Join lines" function doing this if the lines are two let statements :thinking:

flodiebold avatar Jul 04 '25 10:07 flodiebold

I think this is useful (it's a transformation I've made a number of times), but I can't remember all the assists we have, so I'm not sure I'd use it.

lnicola avatar Jul 04 '25 10:07 lnicola

I personally agree with closing this.

ChayimFriedman2 avatar Jul 04 '25 14:07 ChayimFriedman2

Random thought, I could imagine our "Join lines" function doing this if the lines are two let statements 🤔

Implementing it in "Join lines" seems quite complex, And it seems that using "Join lines" requires inputting commands, which is not as simplify as using assist

A4-Tacks avatar Aug 12 '25 08:08 A4-Tacks