From e7abf34c1984196aadc3b3ee6e95887e22b68dea Mon Sep 17 00:00:00 2001 From: Mathew Horner Date: Thu, 15 Sep 2022 20:25:29 -0500 Subject: [PATCH 1/2] Fix add reference action on macros. --- .../src/handlers/type_mismatch.rs | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs index 6bf90e645b4..937f98f4798 100644 --- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs +++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs @@ -59,9 +59,6 @@ fn add_reference( d: &hir::TypeMismatch, acc: &mut Vec, ) -> Option<()> { - let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?; - let expr_node = d.expr.value.to_node(&root); - let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range; let (_, mutability) = d.expected.as_reference()?; @@ -72,7 +69,7 @@ fn add_reference( let ampersands = format!("&{}", mutability.as_keyword_for_ref()); - let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands); + 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)); @@ -314,6 +311,34 @@ fn main() { ); } + #[test] + fn test_add_reference_to_macro_call() { + check_fix( + r#" +macro_rules! hello_world { + () => { + "Hello World".to_string() + }; +} +fn test(foo: &String) {} +fn main() { + test($0hello_world!()); +} + "#, + r#" +macro_rules! hello_world { + () => { + "Hello World".to_string() + }; +} +fn test(foo: &String) {} +fn main() { + test(&hello_world!()); +} + "#, + ); + } + #[test] fn test_add_mutable_reference_to_let_stmt() { check_fix( From a65ca20210b4cf82c32f11249208f990af3fb816 Mon Sep 17 00:00:00 2001 From: Mathew Horner Date: Fri, 16 Sep 2022 16:56:19 -0500 Subject: [PATCH 2/2] Fix tests by using primitive rather than String. --- .../src/handlers/type_mismatch.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs index 937f98f4798..62c69f90baa 100644 --- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs +++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs @@ -315,25 +315,25 @@ fn main() { fn test_add_reference_to_macro_call() { check_fix( r#" -macro_rules! hello_world { +macro_rules! thousand { () => { - "Hello World".to_string() + 1000_u64 }; } -fn test(foo: &String) {} +fn test(foo: &u64) {} fn main() { - test($0hello_world!()); + test($0thousand!()); } "#, r#" -macro_rules! hello_world { +macro_rules! thousand { () => { - "Hello World".to_string() + 1000_u64 }; } -fn test(foo: &String) {} +fn test(foo: &u64) {} fn main() { - test(&hello_world!()); + test(&thousand!()); } "#, );