Unnamed repository; edit this file 'description' to name the repository.
Fix: renaming mut vars removed `mut` in patterns
Renaming a pattern which is generated by a macro removed the mut keyword.
Fixes rust-lang/rust-analyzer#22301
| -rw-r--r-- | crates/ide-db/src/rename.rs | 42 | ||||
| -rw-r--r-- | crates/ide/src/rename.rs | 47 |
2 files changed, 72 insertions, 17 deletions
diff --git a/crates/ide-db/src/rename.rs b/crates/ide-db/src/rename.rs index ac0b099713..ff4d5a2886 100644 --- a/crates/ide-db/src/rename.rs +++ b/crates/ide-db/src/rename.rs @@ -701,28 +701,36 @@ fn source_edit_from_def( for source in local.sources(sema.db) { let source = match source.source.clone().original_ast_node_rooted(sema.db) { Some(source) => source, - None => match source - .source - .syntax() - .original_file_range_opt(sema.db) - .map(TupleExt::head) - { - Some(FileRange { file_id: file_id2, range }) => { - file_id = Some(file_id2); - edit.replace( - range, - new_name.display(sema.db, file_id2.edition(sema.db)).to_string(), - ); - continue; - } - None => { - bail!("Can't rename local that is defined in a macro declaration") + None => { + match source + .as_ident_pat() + .and_then(|x| x.name()) + .and_then(|x| sema.original_range_opt(x.syntax())) + .or_else(|| { + source + .source + .syntax() + .original_file_range_opt(sema.db) + .map(TupleExt::head) + }) { + Some(FileRange { file_id: file_id2, range }) => { + file_id = Some(file_id2); + edit.replace( + range, + new_name.display(sema.db, file_id2.edition(sema.db)).to_string(), + ); + continue; + } + None => { + bail!("Can't rename local that is defined in a macro declaration") + } } - }, + } }; file_id = Some(source.file_id); if let Either::Left(pat) = source.value { let name_range = pat.name().unwrap().syntax().text_range(); + // special cases required for renaming fields/locals in Record patterns if let Some(pat_field) = pat.syntax().parent().and_then(ast::RecordPatField::cast) { if let Some(name_ref) = pat_field.name_ref() { diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index 6d1e141d21..5d86ad5077 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -4079,4 +4079,51 @@ impl<'a, T> Foo<'a, T> {} "#, ); } + + #[test] + fn test_rename_mut_pattern_with_macro() { + check( + "new", + r#" +macro_rules! pat_macro { + ($pat:pat) => { + $pat + }; +} +enum CustomOption<T> { + None, + Some(T), +} + +pub fn main() { + match CustomOption::None { + pat_macro!(CustomOption::Some(mut old$0)) => { + old += 1, + } + None => {} + } +} +"#, + r#" +macro_rules! pat_macro { + ($pat:pat) => { + $pat + }; +} +enum CustomOption<T> { + None, + Some(T), +} + +pub fn main() { + match CustomOption::None { + pat_macro!(CustomOption::Some(mut new)) => { + new += 1, + } + None => {} + } +} +"#, + ); + } } |