Skip to content

Commit e7abf34

Browse files
committed
Fix add reference action on macros.
1 parent 2b61be2 commit e7abf34

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ fn add_reference(
5959
d: &hir::TypeMismatch,
6060
acc: &mut Vec<Assist>,
6161
) -> Option<()> {
62-
let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
63-
let expr_node = d.expr.value.to_node(&root);
64-
6562
let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range;
6663

6764
let (_, mutability) = d.expected.as_reference()?;
@@ -72,7 +69,7 @@ fn add_reference(
7269

7370
let ampersands = format!("&{}", mutability.as_keyword_for_ref());
7471

75-
let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands);
72+
let edit = TextEdit::insert(range.start(), ampersands);
7673
let source_change =
7774
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
7875
acc.push(fix("add_reference_here", "Add reference here", source_change, range));
@@ -314,6 +311,34 @@ fn main() {
314311
);
315312
}
316313

314+
#[test]
315+
fn test_add_reference_to_macro_call() {
316+
check_fix(
317+
r#"
318+
macro_rules! hello_world {
319+
() => {
320+
"Hello World".to_string()
321+
};
322+
}
323+
fn test(foo: &String) {}
324+
fn main() {
325+
test($0hello_world!());
326+
}
327+
"#,
328+
r#"
329+
macro_rules! hello_world {
330+
() => {
331+
"Hello World".to_string()
332+
};
333+
}
334+
fn test(foo: &String) {}
335+
fn main() {
336+
test(&hello_world!());
337+
}
338+
"#,
339+
);
340+
}
341+
317342
#[test]
318343
fn test_add_mutable_reference_to_let_stmt() {
319344
check_fix(

0 commit comments

Comments
 (0)