Skip to content

fix(assists/add_reference_here): _modify_ the reference type when dealing with &T->&mut T#22342

Merged
Veykril merged 3 commits into
rust-lang:masterfrom
ada4a:13047-ref-t-to-refmut-t
May 26, 2026
Merged

fix(assists/add_reference_here): _modify_ the reference type when dealing with &T->&mut T#22342
Veykril merged 3 commits into
rust-lang:masterfrom
ada4a:13047-ref-t-to-refmut-t

Conversation

@ada4a
Copy link
Copy Markdown
Contributor

@ada4a ada4a commented May 11, 2026

..instead of adding another layer of reference, i.e. &T -> &mut &T

Fixes #13047

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 11, 2026
@rustbot

This comment has been minimized.

@ada4a ada4a force-pushed the 13047-ref-t-to-refmut-t branch from 910f6cb to fdfffdb Compare May 11, 2026 18:43
@rustbot

This comment has been minimized.

Comment on lines +100 to +108
let expr = expr_ptr.to_node(ctx.db());
// If the node comes from a macro expansion, then we shouldn't assist,
// as the suggestion would overwrite the macro _definition_ position
let expr = ctx.sema.original_ast_node(expr)?;
let expr_without_ref = RefExpr::cast(expr.syntax().clone())?.expr()?;

let pos = expr_without_ref.syntax().text_range().start();
let edit = TextEdit::insert(pos, "mut ".to_string());
let source_change = SourceChange::from_text_edit(range.file_id, edit);
Copy link
Copy Markdown
Contributor Author

@ada4a ada4a May 11, 2026

Choose a reason for hiding this comment

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

I initially wanted to do this with SyntaxEditor, but it looks like a text edit works just fine... Let me know which one I should go with.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SyntaxEditor would likely be better in the future, but this is fine for now

Copy link
Copy Markdown
Contributor Author

@ada4a ada4a May 26, 2026

Choose a reason for hiding this comment

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

I had actually implemented it with SyntaxEditor initially, see the second commit.. I'll open a PR which switches to that then (EDIT: done, see #22467)

@ada4a ada4a force-pushed the 13047-ref-t-to-refmut-t branch 2 times, most recently from a66b485 to 4e6676e Compare May 11, 2026 18:48
@rustbot

This comment has been minimized.

ada4a and others added 3 commits May 26, 2026 15:11
- use `ctx.db()`
- move `add_mutable_reference_to_let_stmt` closer to `add_reference_to_let_stmt`
…ling with &T->&mut T

..instead of adding another layer of reference, i.e. &T -> &mut &T

Co-authored-by: Ana Hobden <operator@hoverbear.org>
@ada4a ada4a force-pushed the 13047-ref-t-to-refmut-t branch from 4e6676e to 05bc639 Compare May 26, 2026 13:12
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 26, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Copy link
Copy Markdown
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Comment on lines +100 to +108
let expr = expr_ptr.to_node(ctx.db());
// If the node comes from a macro expansion, then we shouldn't assist,
// as the suggestion would overwrite the macro _definition_ position
let expr = ctx.sema.original_ast_node(expr)?;
let expr_without_ref = RefExpr::cast(expr.syntax().clone())?.expr()?;

let pos = expr_without_ref.syntax().text_range().start();
let edit = TextEdit::insert(pos, "mut ".to_string());
let source_change = SourceChange::from_text_edit(range.file_id, edit);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SyntaxEditor would likely be better in the future, but this is fine for now

@Veykril Veykril added this pull request to the merge queue May 26, 2026
Merged via the queue into rust-lang:master with commit c3af07a May 26, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 26, 2026
@ada4a ada4a deleted the 13047-ref-t-to-refmut-t branch May 27, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Add a reference" assist adds an extra ampersand when fixing to a mutable ref

3 participants