Skip to content

Triggering "Add reference here" code action adds an ampersand in the wrong location. #12721

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

Closed
abusch opened this issue Jul 8, 2022 · 2 comments
Labels
A-assists C-bug Category: bug E-has-instructions Issue has some instructions and pointers to code to get started

Comments

@abusch
Copy link

abusch commented Jul 8, 2022

This is basically the exact same symptom as #12717 except it happens when executing the "Add reference here" code action instead of using completion.

The same example as in #12717 can be used to reproduce it. If you type show_value(request.query_id) in the test function at the bottom of the file, then select "Add reference here", the ampersand character is added on line 6 instead of the correct line.

Interestingly, the "consider borrowing here" code action works fine.

rust-analyzer version: rust-analyzer 0.0.0 (75b2232 2022-07-03)

rustc version: rustc 1.62.0 (a8314ef7d 2022-06-27)

@flodiebold flodiebold added A-assists C-bug Category: bug labels Jul 11, 2022
@Veykril
Copy link
Member

Veykril commented Jan 30, 2023

Relevant code is here

fn add_reference(
ctx: &DiagnosticsContext<'_>,
d: &hir::TypeMismatch,
acc: &mut Vec<Assist>,
) -> Option<()> {
let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range;
let (_, mutability) = d.expected.as_reference()?;
let actual_with_ref = Type::reference(&d.actual, mutability);
if !actual_with_ref.could_coerce_to(ctx.sema.db, &d.expected) {
return None;
}
let ampersands = format!("&{}", mutability.as_keyword_for_ref());
let edit = TextEdit::insert(range.start(), ampersands);
let source_change =
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
acc.push(fix("add_reference_here", "Add reference here", source_change, range));
Some(())
}

@Veykril Veykril added the E-has-instructions Issue has some instructions and pointers to code to get started label Jan 30, 2023
@snprajwal
Copy link
Contributor

I can't reproduce this, looks like it has been fixed by the patch for #12717?

@Veykril Veykril closed this as completed Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-bug Category: bug E-has-instructions Issue has some instructions and pointers to code to get started
Projects
None yet
Development

No branches or pull requests

4 participants