Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't add reference when it isn't needed for the "Extract variable" assist #17991

Merged
merged 3 commits into from
Aug 29, 2024

Conversation

ChayimFriedman2
Copy link
Contributor

I.e. don't generate let var_name = &foo(). Because it always irritates me when I need to fix that.

Anything that creates a new value don't need a reference. That excludes mostly field accesses and indexing.

I had a thought that we can also not generate a reference for fields and indexing as long as the type is Copy, but sometimes people impl Copy even when they don't want to copy the values (e.g. a large type), so I didn't do that.

…ssist

I.e. don't generate `let var_name = &foo()`.

Anything that creates a new value don't need a reference. That excludes mostly field accesses and indexing.

I had a thought that we can also not generate a reference for fields and indexing as long as the type is `Copy`, but sometimes people impl `Copy` even when they don't want to copy the values (e.g. a large type), so I didn't do that.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 28, 2024
And BTW, remove the parentheses of the extracted expression if there are.
.as_ref()
.map_or(false, |it| matches!(it, ast::Expr::FieldExpr(_) | ast::Expr::MethodCallExpr(_)));
// Any expression that autoderefs may need adjustment.
let mut needs_adjust = parent.as_ref().map_or(false, |it| match it {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, couldn't we check if the type of let ty = ctx.sema.type_of_expr(&to_extract).map(TypeInfo::adjusted); and let ty = ctx.sema.type_of_expr(&to_extract).map(TypeInfo::original); differs to tell this? I guess we are only interested in autoderef adjustments so probably not

@Veykril
Copy link
Member

Veykril commented Aug 29, 2024

Thanks! This has bothered me for a long time (I wonder if I made an issue regarding that)
@bors r+

@bors
Copy link
Contributor

bors commented Aug 29, 2024

📌 Commit f297860 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 29, 2024

⌛ Testing commit f297860 with merge 5350c65...

@bors
Copy link
Contributor

bors commented Aug 29, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 5350c65 to master...

@bors bors merged commit 5350c65 into rust-lang:master Aug 29, 2024
11 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the extract-variable-ref branch October 16, 2024 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants