Skip to content

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

Open
ada4a wants to merge 3 commits into
rust-lang:masterfrom
ada4a:13047-ref-t-to-refmut-t
Open

fix(assists/add_reference_here): _modify_ the reference type when dealing with &T->&mut T#22342
ada4a wants to merge 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.

- use `ctx.db()`
- move `add_mutable_reference_to_let_stmt` closer to `add_reference_to_let_stmt`
@ada4a ada4a force-pushed the 13047-ref-t-to-refmut-t branch from 910f6cb to fdfffdb Compare May 11, 2026 18:43
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 11, 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.

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

@ada4a ada4a force-pushed the 13047-ref-t-to-refmut-t branch from fdfffdb to a66b485 Compare May 11, 2026 18:46
ada4a and others added 2 commits May 11, 2026 20:48
…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 a66b485 to 4e6676e Compare May 11, 2026 18:48
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.

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

2 participants