1 //! FIXME: write short doc here
7 use hir::{Module, ModuleDef, ModuleSource, Semantics};
8 use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt};
10 defs::{Definition, NameClass, NameRefClass},
14 algo::find_node_at_offset,
15 ast::{self, NameOwner},
16 lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, T,
19 use text_edit::TextEdit;
22 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, ReferenceSearchResult,
23 SourceChange, SourceFileEdit, TextRange, TextSize,
26 type RenameResult<T> = Result<T, RenameError>;
28 pub struct RenameError(pub(crate) String);
30 impl fmt::Display for RenameError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 Display::fmt(&self.0, f)
36 macro_rules! format_err {
37 ($fmt:expr) => {RenameError(format!($fmt))};
38 ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
42 ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))}
45 pub(crate) fn prepare_rename(
47 position: FilePosition,
48 ) -> RenameResult<RangeInfo<()>> {
49 let sema = Semantics::new(db);
50 let source_file = sema.parse(position.file_id);
51 let syntax = source_file.syntax();
52 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
53 rename_mod(&sema, position, module, "dummy")
54 } else if let Some(self_token) =
55 syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
57 rename_self_to_param(&sema, position, self_token, "dummy")
59 let RangeInfo { range, .. } = find_all_refs(&sema, position)?;
60 Ok(RangeInfo::new(range, SourceChange::from(vec![])))
62 .map(|info| RangeInfo::new(info.range, ()))
67 position: FilePosition,
69 ) -> RenameResult<RangeInfo<SourceChange>> {
70 let sema = Semantics::new(db);
71 rename_with_semantics(&sema, position, new_name)
74 pub(crate) fn rename_with_semantics(
75 sema: &Semantics<RootDatabase>,
76 position: FilePosition,
78 ) -> RenameResult<RangeInfo<SourceChange>> {
79 let source_file = sema.parse(position.file_id);
80 let syntax = source_file.syntax();
82 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
83 rename_mod(&sema, position, module, new_name)
84 } else if let Some(self_token) =
85 syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
87 rename_self_to_param(&sema, position, self_token, new_name)
89 rename_reference(&sema, position, new_name)
93 pub(crate) fn will_rename_file(
97 ) -> Option<SourceChange> {
98 let sema = Semantics::new(db);
99 let module = sema.to_module_def(file_id)?;
101 let decl = module.declaration_source(db)?;
102 let range = decl.value.name()?.syntax().text_range();
104 let position = FilePosition { file_id: decl.file_id.original_file(db), offset: range.start() };
105 let mut change = rename_mod(&sema, position, module, new_name_stem).ok()?.info;
106 change.file_system_edits.clear();
110 #[derive(Debug, PartialEq)]
111 enum IdentifierKind {
118 fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
119 match lex_single_syntax_kind(new_name) {
120 Some(res) => match res {
121 (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
122 (T![_], _) => Ok(IdentifierKind::Underscore),
123 (T![self], _) => Ok(IdentifierKind::ToSelf),
124 (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
125 Ok(IdentifierKind::Lifetime)
127 (SyntaxKind::LIFETIME_IDENT, _) => {
128 bail!("Invalid name `{0}`: Cannot rename lifetime to {0}", new_name)
130 (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
131 (_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
133 None => bail!("Invalid name `{}`: not an identifier", new_name),
137 fn find_module_at_offset(
138 sema: &Semantics<RootDatabase>,
139 position: FilePosition,
141 ) -> Option<Module> {
142 let ident = syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::IDENT)?;
144 let module = match_ast! {
145 match (ident.parent()) {
146 ast::NameRef(name_ref) => {
147 match NameRefClass::classify(sema, &name_ref)? {
148 NameRefClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
153 match NameClass::classify(&sema, &name)? {
154 NameClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
166 sema: &Semantics<RootDatabase>,
167 position: FilePosition,
168 ) -> RenameResult<RangeInfo<ReferenceSearchResult>> {
169 crate::references::find_all_refs(sema, position, None)
170 .ok_or_else(|| format_err!("No references found at position"))
173 fn source_edit_from_reference(
174 sema: &Semantics<RootDatabase>,
175 reference: Reference,
177 ) -> SourceFileEdit {
178 let mut replacement_text = String::new();
179 let range = match reference.kind {
180 ReferenceKind::FieldShorthandForField => {
181 mark::hit!(test_rename_struct_field_for_shorthand);
182 replacement_text.push_str(new_name);
183 replacement_text.push_str(": ");
184 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start())
186 ReferenceKind::FieldShorthandForLocal => {
187 mark::hit!(test_rename_local_for_field_shorthand);
188 replacement_text.push_str(": ");
189 replacement_text.push_str(new_name);
190 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
192 ReferenceKind::RecordFieldExprOrPat => {
193 mark::hit!(test_rename_field_expr_pat);
194 replacement_text.push_str(new_name);
195 edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name)
198 replacement_text.push_str(new_name);
199 reference.file_range.range
203 file_id: reference.file_range.file_id,
204 edit: TextEdit::replace(range, replacement_text),
208 fn edit_text_range_for_record_field_expr_or_pat(
209 sema: &Semantics<RootDatabase>,
210 file_range: FileRange,
213 let source_file = sema.parse(file_range.file_id);
214 let file_syntax = source_file.syntax();
215 let original_range = file_range.range;
217 syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
218 .and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
219 Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
223 syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
224 .and_then(|field_pat| match field_pat.pat() {
225 Some(ast::Pat::IdentPat(pat))
226 if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
228 Some(field_pat.syntax().text_range())
233 .unwrap_or(original_range)
237 sema: &Semantics<RootDatabase>,
238 position: FilePosition,
241 ) -> RenameResult<RangeInfo<SourceChange>> {
242 if IdentifierKind::Ident != check_identifier(new_name)? {
243 bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
245 let mut source_file_edits = Vec::new();
246 let mut file_system_edits = Vec::new();
248 let src = module.definition_source(sema.db);
249 let file_id = src.file_id.original_file(sema.db);
251 ModuleSource::SourceFile(..) => {
252 // mod is defined in path/to/dir/mod.rs
253 let path = if module.is_mod_rs(sema.db) {
254 format!("../{}/mod.rs", new_name)
256 format!("{}.rs", new_name)
258 let dst = AnchoredPathBuf { anchor: file_id, path };
259 let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
260 file_system_edits.push(move_file);
262 ModuleSource::Module(..) => {}
265 if let Some(src) = module.declaration_source(sema.db) {
266 let file_id = src.file_id.original_file(sema.db);
267 let name = src.value.name().unwrap();
268 let edit = SourceFileEdit {
270 edit: TextEdit::replace(name.syntax().text_range(), new_name.into()),
272 source_file_edits.push(edit);
275 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
279 .map(|reference| source_edit_from_reference(sema, reference, new_name));
280 source_file_edits.extend(ref_edits);
282 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
286 sema: &Semantics<RootDatabase>,
287 position: FilePosition,
288 ) -> Result<RangeInfo<SourceChange>, RenameError> {
289 let source_file = sema.parse(position.file_id);
290 let syn = source_file.syntax();
292 let (fn_def, fn_ast) = find_node_at_offset::<ast::Fn>(syn, position.offset)
293 .and_then(|fn_ast| sema.to_def(&fn_ast).zip(Some(fn_ast)))
294 .ok_or_else(|| format_err!("No surrounding method declaration found"))?;
295 let param_range = fn_ast
297 .and_then(|p| p.params().next())
298 .ok_or_else(|| format_err!("Method has no parameters"))?
301 if !param_range.contains(position.offset) {
302 bail!("Only the first parameter can be self");
305 let impl_block = find_node_at_offset::<ast::Impl>(syn, position.offset)
306 .and_then(|def| sema.to_def(&def))
307 .ok_or_else(|| format_err!("No impl block found for function"))?;
308 if fn_def.self_param(sema.db).is_some() {
309 bail!("Method already has a self parameter");
312 let params = fn_def.assoc_fn_params(sema.db);
313 let first_param = params.first().ok_or_else(|| format_err!("Method has no parameters"))?;
314 let first_param_ty = first_param.ty();
315 let impl_ty = impl_block.target_ty(sema.db);
316 let (ty, self_param) = if impl_ty.remove_ref().is_some() {
317 // if the impl is a ref to the type we can just match the `&T` with self directly
318 (first_param_ty.clone(), "self")
320 first_param_ty.remove_ref().map_or((first_param_ty.clone(), "self"), |ty| {
321 (ty, if first_param_ty.is_mutable_reference() { "&mut self" } else { "&self" })
326 bail!("Parameter type differs from impl block type");
329 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
331 let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs
333 .partition(|reference| param_range.intersect(reference.file_range.range).is_some());
335 if param_ref.is_empty() {
336 bail!("Parameter to rename not found");
339 let mut edits = usages
341 .map(|reference| source_edit_from_reference(sema, reference, "self"))
342 .collect::<Vec<_>>();
344 edits.push(SourceFileEdit {
345 file_id: position.file_id,
346 edit: TextEdit::replace(param_range, String::from(self_param)),
349 Ok(RangeInfo::new(range, SourceChange::from(edits)))
352 fn text_edit_from_self_param(
354 self_param: &ast::SelfParam,
356 ) -> Option<TextEdit> {
357 fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
358 if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
359 return Some(p.path()?.segment()?.name_ref()?.text().to_string());
364 let impl_def = find_node_at_offset::<ast::Impl>(syn, self_param.syntax().text_range().start())?;
365 let type_name = target_type_name(&impl_def)?;
367 let mut replacement_text = String::from(new_name);
368 replacement_text.push_str(": ");
369 match (self_param.amp_token(), self_param.mut_token()) {
371 (Some(_), None) => replacement_text.push('&'),
372 (_, Some(_)) => replacement_text.push_str("&mut "),
374 replacement_text.push_str(type_name.as_str());
376 Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
379 fn rename_self_to_param(
380 sema: &Semantics<RootDatabase>,
381 position: FilePosition,
382 self_token: SyntaxToken,
384 ) -> Result<RangeInfo<SourceChange>, RenameError> {
385 let ident_kind = check_identifier(new_name)?;
387 IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
388 IdentifierKind::ToSelf => {
390 mark::hit!(rename_self_to_self);
391 return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default()));
395 let source_file = sema.parse(position.file_id);
396 let syn = source_file.syntax();
398 let text = sema.db.file_text(position.file_id);
399 let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset)
400 .ok_or_else(|| format_err!("No surrounding method declaration found"))?;
401 let search_range = fn_def.syntax().text_range();
403 let mut edits: Vec<SourceFileEdit> = vec![];
405 for (idx, _) in text.match_indices("self") {
406 let offset: TextSize = idx.try_into().unwrap();
407 if !search_range.contains_inclusive(offset) {
410 if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) {
411 let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) {
412 text_edit_from_self_param(syn, self_param, new_name)
413 .ok_or_else(|| format_err!("No target type found"))?
415 TextEdit::replace(usage.text_range(), String::from(new_name))
417 edits.push(SourceFileEdit { file_id: position.file_id, edit });
421 if edits.len() > 1 && ident_kind == IdentifierKind::Underscore {
422 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
425 let range = ast::SelfParam::cast(self_token.parent())
426 .map_or(self_token.text_range(), |p| p.syntax().text_range());
428 Ok(RangeInfo::new(range, SourceChange::from(edits)))
432 sema: &Semantics<RootDatabase>,
433 position: FilePosition,
435 ) -> Result<RangeInfo<SourceChange>, RenameError> {
436 let ident_kind = check_identifier(new_name)?;
437 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
439 match (ident_kind, &refs.declaration.kind) {
440 (IdentifierKind::ToSelf, ReferenceKind::Lifetime)
441 | (IdentifierKind::Underscore, ReferenceKind::Lifetime)
442 | (IdentifierKind::Ident, ReferenceKind::Lifetime) => {
443 mark::hit!(rename_not_a_lifetime_ident_ref);
444 bail!("Invalid name `{}`: not a lifetime identifier", new_name)
446 (IdentifierKind::Lifetime, ReferenceKind::Lifetime) => mark::hit!(rename_lifetime),
447 (IdentifierKind::Lifetime, _) => {
448 mark::hit!(rename_not_an_ident_ref);
449 bail!("Invalid name `{}`: not an identifier", new_name)
451 (IdentifierKind::ToSelf, ReferenceKind::SelfKw) => {
452 unreachable!("rename_self_to_param should've been called instead")
454 (IdentifierKind::ToSelf, _) => {
455 mark::hit!(rename_to_self);
456 return rename_to_self(sema, position);
458 (IdentifierKind::Underscore, _) if !refs.references.is_empty() => {
459 mark::hit!(rename_underscore_multiple);
460 bail!("Cannot rename reference to `_` as it is being referenced multiple times")
462 (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
467 .map(|reference| source_edit_from_reference(sema, reference, new_name))
468 .collect::<Vec<_>>();
470 Ok(RangeInfo::new(range, SourceChange::from(edit)))
475 use expect_test::{expect, Expect};
476 use stdx::trim_indent;
477 use test_utils::{assert_eq_text, mark};
478 use text_edit::TextEdit;
480 use crate::{fixture, FileId};
482 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
483 let ra_fixture_after = &trim_indent(ra_fixture_after);
484 let (analysis, position) = fixture::position(ra_fixture_before);
485 let rename_result = analysis
486 .rename(position, new_name)
487 .unwrap_or_else(|err| panic!("Rename to '{}' was cancelled: {}", new_name, err));
488 match rename_result {
489 Ok(source_change) => {
490 let mut text_edit_builder = TextEdit::builder();
491 let mut file_id: Option<FileId> = None;
492 for edit in source_change.info.source_file_edits {
493 file_id = Some(edit.file_id);
494 for indel in edit.edit.into_iter() {
495 text_edit_builder.replace(indel.delete, indel.insert);
498 if let Some(file_id) = file_id {
499 let mut result = analysis.file_text(file_id).unwrap().to_string();
500 text_edit_builder.finish().apply(&mut result);
501 assert_eq_text!(ra_fixture_after, &*result);
505 if ra_fixture_after.starts_with("error:") {
506 let error_message = ra_fixture_after
509 .skip("error:".len())
510 .collect::<String>();
511 assert_eq!(error_message.trim(), err.to_string());
514 panic!("Rename to '{}' failed unexpectedly: {}", new_name, err)
520 fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
521 let (analysis, position) = fixture::position(ra_fixture);
522 let source_change = analysis
523 .rename(position, new_name)
525 .expect("Expect returned RangeInfo to be Some, but was None");
526 expect.assert_debug_eq(&source_change)
530 fn test_rename_to_underscore() {
531 check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#);
535 fn test_rename_to_raw_identifier() {
536 check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
540 fn test_rename_to_invalid_identifier1() {
543 r#"fn main() { let i$0 = 1; }"#,
544 "error: Invalid name `invalid!`: not an identifier",
549 fn test_rename_to_invalid_identifier2() {
552 r#"fn main() { let i$0 = 1; }"#,
553 "error: Invalid name `multiple tokens`: not an identifier",
558 fn test_rename_to_invalid_identifier3() {
561 r#"fn main() { let i$0 = 1; }"#,
562 "error: Invalid name `let`: not an identifier",
567 fn test_rename_to_invalid_identifier_lifetime() {
568 mark::check!(rename_not_an_ident_ref);
571 r#"fn main() { let i$0 = 1; }"#,
572 "error: Invalid name `'foo`: not an identifier",
577 fn test_rename_to_invalid_identifier_lifetime2() {
578 mark::check!(rename_not_a_lifetime_ident_ref);
581 r#"fn main<'a>(_: &'a$0 ()) {}"#,
582 "error: Invalid name `foo`: not a lifetime identifier",
587 fn test_rename_to_underscore_invalid() {
588 mark::check!(rename_underscore_multiple);
591 r#"fn main(foo$0: ()) {foo;}"#,
592 "error: Cannot rename reference to `_` as it is being referenced multiple times",
597 fn test_rename_mod_invalid() {
601 "error: Invalid name `'foo`: cannot rename module to 'foo",
606 fn test_rename_for_local() {
607 mark::check!(rename_ident);
636 fn test_rename_unresolved_reference() {
639 r#"fn main() { let _ = unresolved_ref$0; }"#,
640 "error: No references found at position",
645 fn test_rename_for_macro_args() {
649 macro_rules! foo {($i:ident) => {$i} }
656 macro_rules! foo {($i:ident) => {$i} }
666 fn test_rename_for_macro_args_rev() {
670 macro_rules! foo {($i:ident) => {$i} }
677 macro_rules! foo {($i:ident) => {$i} }
687 fn test_rename_for_macro_define_fn() {
691 macro_rules! define_fn {($id:ident) => { fn $id{} }}
698 macro_rules! define_fn {($id:ident) => { fn $id{} }}
708 fn test_rename_for_macro_define_fn_rev() {
712 macro_rules! define_fn {($id:ident) => { fn $id{} }}
719 macro_rules! define_fn {($id:ident) => { fn $id{} }}
729 fn test_rename_for_param_inside() {
730 check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#);
734 fn test_rename_refs_for_fn_param() {
735 check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
739 fn test_rename_for_mut_param() {
740 check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
744 fn test_rename_struct_field() {
748 struct Foo { i$0: i32 }
751 fn new(i: i32) -> Self {
757 struct Foo { j: i32 }
760 fn new(i: i32) -> Self {
769 fn test_rename_struct_field_for_shorthand() {
770 mark::check!(test_rename_struct_field_for_shorthand);
774 struct Foo { i$0: i32 }
777 fn new(i: i32) -> Self {
783 struct Foo { j: i32 }
786 fn new(i: i32) -> Self {
795 fn test_rename_local_for_field_shorthand() {
796 mark::check!(test_rename_local_for_field_shorthand);
800 struct Foo { i: i32 }
803 fn new(i$0: i32) -> Self {
809 struct Foo { i: i32 }
812 fn new(j: i32) -> Self {
821 fn test_field_shorthand_correct_struct() {
825 struct Foo { i$0: i32 }
826 struct Bar { i: i32 }
829 fn new(i: i32) -> Self {
835 struct Foo { j: i32 }
836 struct Bar { i: i32 }
839 fn new(i: i32) -> Self {
848 fn test_shadow_local_for_struct_shorthand() {
852 struct Foo { i: i32 }
854 fn baz(i$0: i32) -> Self {
863 struct Foo { i: i32 }
865 fn baz(j: i32) -> Self {
866 let x = Foo { i: j };
877 fn test_rename_mod() {
914 dst: AnchoredPathBuf {
930 fn test_rename_mod_in_use_tree() {
940 pub struct FooContent;
943 use crate::foo$0::FooContent;
982 dst: AnchoredPathBuf {
998 fn test_rename_mod_in_dir() {
1010 info: SourceChange {
1011 source_file_edits: [
1026 file_system_edits: [
1031 dst: AnchoredPathBuf {
1035 path: "../foo2/mod.rs",
1047 fn test_rename_unusually_nested_mod() {
1052 mod outer { mod fo$0o; }
1060 info: SourceChange {
1061 source_file_edits: [
1076 file_system_edits: [
1081 dst: AnchoredPathBuf {
1097 fn test_module_rename_in_path() {
1101 mod $0foo { pub fn bar() {} }
1103 fn main() { foo::bar(); }
1106 mod baz { pub fn bar() {} }
1108 fn main() { baz::bar(); }
1114 fn test_rename_mod_filename_and_path() {
1133 info: SourceChange {
1134 source_file_edits: [
1162 file_system_edits: [
1167 dst: AnchoredPathBuf {
1183 fn test_enum_variant_from_module_1() {
1188 pub enum Foo { Bar$0 }
1191 fn func(f: foo::Foo) {
1199 pub enum Foo { Baz }
1202 fn func(f: foo::Foo) {
1212 fn test_enum_variant_from_module_2() {
1217 pub struct Foo { pub bar$0: uint }
1220 fn foo(f: foo::Foo) {
1226 pub struct Foo { pub baz: uint }
1229 fn foo(f: foo::Foo) {
1237 fn test_parameter_to_self() {
1238 mark::check!(rename_to_self);
1242 struct Foo { i: i32 }
1245 fn f(foo$0: &mut Foo) -> i32 {
1251 struct Foo { i: i32 }
1254 fn f(&mut self) -> i32 {
1263 struct Foo { i: i32 }
1266 fn f(foo$0: Foo) -> i32 {
1272 struct Foo { i: i32 }
1284 fn test_parameter_to_self_error_no_impl() {
1288 struct Foo { i: i32 }
1290 fn f(foo$0: &mut Foo) -> i32 {
1294 "error: No impl block found for function",
1299 struct Foo { i: i32 }
1303 fn f(foo$0: &mut Foo) -> i32 {
1308 "error: Parameter type differs from impl block type",
1313 fn test_parameter_to_self_error_not_first() {
1317 struct Foo { i: i32 }
1319 fn f(x: (), foo$0: &mut Foo) -> i32 {
1324 "error: Only the first parameter can be self",
1329 fn test_parameter_to_self_impl_ref() {
1333 struct Foo { i: i32 }
1335 fn f(foo$0: &Foo) -> i32 {
1341 struct Foo { i: i32 }
1352 fn test_self_to_parameter() {
1356 struct Foo { i: i32 }
1359 fn f(&mut $0self) -> i32 {
1365 struct Foo { i: i32 }
1368 fn f(foo: &mut Foo) -> i32 {
1377 fn test_owned_self_to_parameter() {
1381 struct Foo { i: i32 }
1384 fn f($0self) -> i32 {
1390 struct Foo { i: i32 }
1393 fn f(foo: Foo) -> i32 {
1402 fn test_self_in_path_to_parameter() {
1406 struct Foo { i: i32 }
1409 fn f(&self) -> i32 {
1416 struct Foo { i: i32 }
1419 fn f(foo: &Foo) -> i32 {
1429 fn test_initializer_use_field_init_shorthand() {
1430 mark::check!(test_rename_field_expr_pat);
1434 struct Foo { i$0: i32 }
1436 fn foo(bar: i32) -> Foo {
1441 struct Foo { bar: i32 }
1443 fn foo(bar: i32) -> Foo {
1451 fn test_struct_field_destructure_into_shorthand() {
1455 struct Foo { i$0: i32 }
1458 let Foo { i: baz } = foo;
1463 struct Foo { baz: i32 }
1466 let Foo { baz } = foo;
1474 fn test_rename_binding_in_destructure_pat() {
1475 let expected_fixture = r#"
1481 let Foo { i: bar } = foo;
1493 let Foo { i: b } = foo;
1507 let Foo { i } = foo;
1516 fn test_rename_binding_in_destructure_param_pat() {
1524 fn foo(Foo { i }: foo) -> i32 {
1533 fn foo(Foo { i: bar }: foo) -> i32 {
1541 fn test_rename_lifetimes() {
1542 mark::check!(rename_lifetime);
1549 impl<'a> Foo<'a> for &'a () {
1550 fn foo() -> &'a$0 () {
1559 impl<'yeeee> Foo<'yeeee> for &'yeeee () {
1560 fn foo() -> &'yeeee () {
1569 fn test_rename_bind_pat() {
1574 enum CustomOption<T> {
1579 let test_variable = CustomOption::Some(22);
1581 match test_variable {
1582 CustomOption::Some(foo$0) if foo == 11 => {}
1588 enum CustomOption<T> {
1593 let test_variable = CustomOption::Some(22);
1595 match test_variable {
1596 CustomOption::Some(new_name) if new_name == 11 => {}
1604 fn test_rename_label() {
1608 fn foo<'a>() -> &'a () {
1617 fn foo<'a>() -> &'a () {
1629 fn test_self_to_self() {
1630 mark::check!(rename_self_to_self);