1 //! FIXME: write short doc here
8 use hir::{Module, ModuleDef, ModuleSource, Semantics};
9 use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt};
11 defs::{Definition, NameClass, NameRefClass},
15 algo::find_node_at_offset,
16 ast::{self, NameOwner},
17 lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, T,
20 use text_edit::TextEdit;
23 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, ReferenceSearchResult,
24 SourceChange, SourceFileEdit, TextRange, TextSize,
27 type RenameResult<T> = Result<T, RenameError>;
29 pub struct RenameError(pub(crate) String);
31 impl fmt::Display for RenameError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 Display::fmt(&self.0, f)
37 impl Error for RenameError {}
39 macro_rules! format_err {
40 ($fmt:expr) => {RenameError(format!($fmt))};
41 ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
45 ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))}
48 pub(crate) fn prepare_rename(
50 position: FilePosition,
51 ) -> RenameResult<RangeInfo<()>> {
52 let sema = Semantics::new(db);
53 let source_file = sema.parse(position.file_id);
54 let syntax = source_file.syntax();
55 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
56 rename_mod(&sema, position, module, "dummy")
57 } else if let Some(self_token) =
58 syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
60 rename_self_to_param(&sema, position, self_token, "dummy")
62 let RangeInfo { range, .. } = find_all_refs(&sema, position)?;
63 Ok(RangeInfo::new(range, SourceChange::from(vec![])))
65 .map(|info| RangeInfo::new(info.range, ()))
70 position: FilePosition,
72 ) -> RenameResult<RangeInfo<SourceChange>> {
73 let sema = Semantics::new(db);
74 rename_with_semantics(&sema, position, new_name)
77 pub(crate) fn rename_with_semantics(
78 sema: &Semantics<RootDatabase>,
79 position: FilePosition,
81 ) -> RenameResult<RangeInfo<SourceChange>> {
82 let source_file = sema.parse(position.file_id);
83 let syntax = source_file.syntax();
85 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
86 rename_mod(&sema, position, module, new_name)
87 } else if let Some(self_token) =
88 syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
90 rename_self_to_param(&sema, position, self_token, new_name)
92 rename_reference(&sema, position, new_name)
96 pub(crate) fn will_rename_file(
100 ) -> Option<SourceChange> {
101 let sema = Semantics::new(db);
102 let module = sema.to_module_def(file_id)?;
104 let decl = module.declaration_source(db)?;
105 let range = decl.value.name()?.syntax().text_range();
107 let position = FilePosition { file_id: decl.file_id.original_file(db), offset: range.start() };
108 let mut change = rename_mod(&sema, position, module, new_name_stem).ok()?.info;
109 change.file_system_edits.clear();
113 #[derive(Debug, PartialEq)]
114 enum IdentifierKind {
121 fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
122 match lex_single_syntax_kind(new_name) {
123 Some(res) => match res {
124 (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
125 (T![_], _) => Ok(IdentifierKind::Underscore),
126 (T![self], _) => Ok(IdentifierKind::ToSelf),
127 (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
128 Ok(IdentifierKind::Lifetime)
130 (SyntaxKind::LIFETIME_IDENT, _) => {
131 bail!("Invalid name `{0}`: Cannot rename lifetime to {0}", new_name)
133 (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
134 (_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
136 None => bail!("Invalid name `{}`: not an identifier", new_name),
140 fn find_module_at_offset(
141 sema: &Semantics<RootDatabase>,
142 position: FilePosition,
144 ) -> Option<Module> {
145 let ident = syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::IDENT)?;
147 let module = match_ast! {
148 match (ident.parent()) {
149 ast::NameRef(name_ref) => {
150 match NameRefClass::classify(sema, &name_ref)? {
151 NameRefClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
156 match NameClass::classify(&sema, &name)? {
157 NameClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
169 sema: &Semantics<RootDatabase>,
170 position: FilePosition,
171 ) -> RenameResult<RangeInfo<ReferenceSearchResult>> {
172 crate::references::find_all_refs(sema, position, None)
173 .ok_or_else(|| format_err!("No references found at position"))
176 fn source_edit_from_reference(
177 sema: &Semantics<RootDatabase>,
178 reference: Reference,
180 ) -> SourceFileEdit {
181 let mut replacement_text = String::new();
182 let range = match reference.kind {
183 ReferenceKind::FieldShorthandForField => {
184 mark::hit!(test_rename_struct_field_for_shorthand);
185 replacement_text.push_str(new_name);
186 replacement_text.push_str(": ");
187 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start())
189 ReferenceKind::FieldShorthandForLocal => {
190 mark::hit!(test_rename_local_for_field_shorthand);
191 replacement_text.push_str(": ");
192 replacement_text.push_str(new_name);
193 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
195 ReferenceKind::RecordFieldExprOrPat => {
196 mark::hit!(test_rename_field_expr_pat);
197 replacement_text.push_str(new_name);
198 edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name)
201 replacement_text.push_str(new_name);
202 reference.file_range.range
206 file_id: reference.file_range.file_id,
207 edit: TextEdit::replace(range, replacement_text),
211 fn edit_text_range_for_record_field_expr_or_pat(
212 sema: &Semantics<RootDatabase>,
213 file_range: FileRange,
216 let source_file = sema.parse(file_range.file_id);
217 let file_syntax = source_file.syntax();
218 let original_range = file_range.range;
220 syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
221 .and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
222 Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
226 syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
227 .and_then(|field_pat| match field_pat.pat() {
228 Some(ast::Pat::IdentPat(pat))
229 if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
231 Some(field_pat.syntax().text_range())
236 .unwrap_or(original_range)
240 sema: &Semantics<RootDatabase>,
241 position: FilePosition,
244 ) -> RenameResult<RangeInfo<SourceChange>> {
245 if IdentifierKind::Ident != check_identifier(new_name)? {
246 bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
248 let mut source_file_edits = Vec::new();
249 let mut file_system_edits = Vec::new();
251 let src = module.definition_source(sema.db);
252 let file_id = src.file_id.original_file(sema.db);
254 ModuleSource::SourceFile(..) => {
255 // mod is defined in path/to/dir/mod.rs
256 let path = if module.is_mod_rs(sema.db) {
257 format!("../{}/mod.rs", new_name)
259 format!("{}.rs", new_name)
261 let dst = AnchoredPathBuf { anchor: file_id, path };
262 let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
263 file_system_edits.push(move_file);
265 ModuleSource::Module(..) => {}
268 if let Some(src) = module.declaration_source(sema.db) {
269 let file_id = src.file_id.original_file(sema.db);
270 let name = src.value.name().unwrap();
271 let edit = SourceFileEdit {
273 edit: TextEdit::replace(name.syntax().text_range(), new_name.into()),
275 source_file_edits.push(edit);
278 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
282 .map(|reference| source_edit_from_reference(sema, reference, new_name));
283 source_file_edits.extend(ref_edits);
285 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
289 sema: &Semantics<RootDatabase>,
290 position: FilePosition,
291 ) -> Result<RangeInfo<SourceChange>, RenameError> {
292 let source_file = sema.parse(position.file_id);
293 let syn = source_file.syntax();
295 let (fn_def, fn_ast) = find_node_at_offset::<ast::Fn>(syn, position.offset)
296 .and_then(|fn_ast| sema.to_def(&fn_ast).zip(Some(fn_ast)))
297 .ok_or_else(|| format_err!("No surrounding method declaration found"))?;
298 let param_range = fn_ast
300 .and_then(|p| p.params().next())
301 .ok_or_else(|| format_err!("Method has no parameters"))?
304 if !param_range.contains(position.offset) {
305 bail!("Only the first parameter can be self");
308 let impl_block = find_node_at_offset::<ast::Impl>(syn, position.offset)
309 .and_then(|def| sema.to_def(&def))
310 .ok_or_else(|| format_err!("No impl block found for function"))?;
311 if fn_def.self_param(sema.db).is_some() {
312 bail!("Method already has a self parameter");
315 let params = fn_def.assoc_fn_params(sema.db);
316 let first_param = params.first().ok_or_else(|| format_err!("Method has no parameters"))?;
317 let first_param_ty = first_param.ty();
318 let impl_ty = impl_block.target_ty(sema.db);
319 let (ty, self_param) = if impl_ty.remove_ref().is_some() {
320 // if the impl is a ref to the type we can just match the `&T` with self directly
321 (first_param_ty.clone(), "self")
323 first_param_ty.remove_ref().map_or((first_param_ty.clone(), "self"), |ty| {
324 (ty, if first_param_ty.is_mutable_reference() { "&mut self" } else { "&self" })
329 bail!("Parameter type differs from impl block type");
332 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
334 let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs
336 .partition(|reference| param_range.intersect(reference.file_range.range).is_some());
338 if param_ref.is_empty() {
339 bail!("Parameter to rename not found");
342 let mut edits = usages
344 .map(|reference| source_edit_from_reference(sema, reference, "self"))
345 .collect::<Vec<_>>();
347 edits.push(SourceFileEdit {
348 file_id: position.file_id,
349 edit: TextEdit::replace(param_range, String::from(self_param)),
352 Ok(RangeInfo::new(range, SourceChange::from(edits)))
355 fn text_edit_from_self_param(
357 self_param: &ast::SelfParam,
359 ) -> Option<TextEdit> {
360 fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
361 if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
362 return Some(p.path()?.segment()?.name_ref()?.text().to_string());
367 let impl_def = find_node_at_offset::<ast::Impl>(syn, self_param.syntax().text_range().start())?;
368 let type_name = target_type_name(&impl_def)?;
370 let mut replacement_text = String::from(new_name);
371 replacement_text.push_str(": ");
372 match (self_param.amp_token(), self_param.mut_token()) {
374 (Some(_), None) => replacement_text.push('&'),
375 (_, Some(_)) => replacement_text.push_str("&mut "),
377 replacement_text.push_str(type_name.as_str());
379 Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
382 fn rename_self_to_param(
383 sema: &Semantics<RootDatabase>,
384 position: FilePosition,
385 self_token: SyntaxToken,
387 ) -> Result<RangeInfo<SourceChange>, RenameError> {
388 let ident_kind = check_identifier(new_name)?;
390 IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
391 IdentifierKind::ToSelf => {
393 mark::hit!(rename_self_to_self);
394 return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default()));
398 let source_file = sema.parse(position.file_id);
399 let syn = source_file.syntax();
401 let text = sema.db.file_text(position.file_id);
402 let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset)
403 .ok_or_else(|| format_err!("No surrounding method declaration found"))?;
404 let search_range = fn_def.syntax().text_range();
406 let mut edits: Vec<SourceFileEdit> = vec![];
408 for (idx, _) in text.match_indices("self") {
409 let offset: TextSize = idx.try_into().unwrap();
410 if !search_range.contains_inclusive(offset) {
413 if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) {
414 let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) {
415 text_edit_from_self_param(syn, self_param, new_name)
416 .ok_or_else(|| format_err!("No target type found"))?
418 TextEdit::replace(usage.text_range(), String::from(new_name))
420 edits.push(SourceFileEdit { file_id: position.file_id, edit });
424 if edits.len() > 1 && ident_kind == IdentifierKind::Underscore {
425 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
428 let range = ast::SelfParam::cast(self_token.parent())
429 .map_or(self_token.text_range(), |p| p.syntax().text_range());
431 Ok(RangeInfo::new(range, SourceChange::from(edits)))
435 sema: &Semantics<RootDatabase>,
436 position: FilePosition,
438 ) -> Result<RangeInfo<SourceChange>, RenameError> {
439 let ident_kind = check_identifier(new_name)?;
440 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
442 match (ident_kind, &refs.declaration.kind) {
443 (IdentifierKind::ToSelf, ReferenceKind::Lifetime)
444 | (IdentifierKind::Underscore, ReferenceKind::Lifetime)
445 | (IdentifierKind::Ident, ReferenceKind::Lifetime) => {
446 mark::hit!(rename_not_a_lifetime_ident_ref);
447 bail!("Invalid name `{}`: not a lifetime identifier", new_name)
449 (IdentifierKind::Lifetime, ReferenceKind::Lifetime) => mark::hit!(rename_lifetime),
450 (IdentifierKind::Lifetime, _) => {
451 mark::hit!(rename_not_an_ident_ref);
452 bail!("Invalid name `{}`: not an identifier", new_name)
454 (IdentifierKind::ToSelf, ReferenceKind::SelfKw) => {
455 unreachable!("rename_self_to_param should've been called instead")
457 (IdentifierKind::ToSelf, _) => {
458 mark::hit!(rename_to_self);
459 return rename_to_self(sema, position);
461 (IdentifierKind::Underscore, _) if !refs.references.is_empty() => {
462 mark::hit!(rename_underscore_multiple);
463 bail!("Cannot rename reference to `_` as it is being referenced multiple times")
465 (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
470 .map(|reference| source_edit_from_reference(sema, reference, new_name))
471 .collect::<Vec<_>>();
473 Ok(RangeInfo::new(range, SourceChange::from(edit)))
478 use expect_test::{expect, Expect};
479 use stdx::trim_indent;
480 use test_utils::{assert_eq_text, mark};
481 use text_edit::TextEdit;
483 use crate::{fixture, FileId};
485 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
486 let ra_fixture_after = &trim_indent(ra_fixture_after);
487 let (analysis, position) = fixture::position(ra_fixture_before);
488 let rename_result = analysis
489 .rename(position, new_name)
490 .unwrap_or_else(|err| panic!("Rename to '{}' was cancelled: {}", new_name, err));
491 match rename_result {
492 Ok(source_change) => {
493 let mut text_edit_builder = TextEdit::builder();
494 let mut file_id: Option<FileId> = None;
495 for edit in source_change.info.source_file_edits {
496 file_id = Some(edit.file_id);
497 for indel in edit.edit.into_iter() {
498 text_edit_builder.replace(indel.delete, indel.insert);
501 if let Some(file_id) = file_id {
502 let mut result = analysis.file_text(file_id).unwrap().to_string();
503 text_edit_builder.finish().apply(&mut result);
504 assert_eq_text!(ra_fixture_after, &*result);
508 if ra_fixture_after.starts_with("error:") {
509 let error_message = ra_fixture_after
512 .skip("error:".len())
513 .collect::<String>();
514 assert_eq!(error_message.trim(), err.to_string());
517 panic!("Rename to '{}' failed unexpectedly: {}", new_name, err)
523 fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
524 let (analysis, position) = fixture::position(ra_fixture);
525 let source_change = analysis
526 .rename(position, new_name)
528 .expect("Expect returned RangeInfo to be Some, but was None");
529 expect.assert_debug_eq(&source_change)
533 fn test_rename_to_underscore() {
534 check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#);
538 fn test_rename_to_raw_identifier() {
539 check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
543 fn test_rename_to_invalid_identifier1() {
546 r#"fn main() { let i$0 = 1; }"#,
547 "error: Invalid name `invalid!`: not an identifier",
552 fn test_rename_to_invalid_identifier2() {
555 r#"fn main() { let i$0 = 1; }"#,
556 "error: Invalid name `multiple tokens`: not an identifier",
561 fn test_rename_to_invalid_identifier3() {
564 r#"fn main() { let i$0 = 1; }"#,
565 "error: Invalid name `let`: not an identifier",
570 fn test_rename_to_invalid_identifier_lifetime() {
571 mark::check!(rename_not_an_ident_ref);
574 r#"fn main() { let i$0 = 1; }"#,
575 "error: Invalid name `'foo`: not an identifier",
580 fn test_rename_to_invalid_identifier_lifetime2() {
581 mark::check!(rename_not_a_lifetime_ident_ref);
584 r#"fn main<'a>(_: &'a$0 ()) {}"#,
585 "error: Invalid name `foo`: not a lifetime identifier",
590 fn test_rename_to_underscore_invalid() {
591 mark::check!(rename_underscore_multiple);
594 r#"fn main(foo$0: ()) {foo;}"#,
595 "error: Cannot rename reference to `_` as it is being referenced multiple times",
600 fn test_rename_mod_invalid() {
604 "error: Invalid name `'foo`: cannot rename module to 'foo",
609 fn test_rename_for_local() {
610 mark::check!(rename_ident);
639 fn test_rename_unresolved_reference() {
642 r#"fn main() { let _ = unresolved_ref$0; }"#,
643 "error: No references found at position",
648 fn test_rename_for_macro_args() {
652 macro_rules! foo {($i:ident) => {$i} }
659 macro_rules! foo {($i:ident) => {$i} }
669 fn test_rename_for_macro_args_rev() {
673 macro_rules! foo {($i:ident) => {$i} }
680 macro_rules! foo {($i:ident) => {$i} }
690 fn test_rename_for_macro_define_fn() {
694 macro_rules! define_fn {($id:ident) => { fn $id{} }}
701 macro_rules! define_fn {($id:ident) => { fn $id{} }}
711 fn test_rename_for_macro_define_fn_rev() {
715 macro_rules! define_fn {($id:ident) => { fn $id{} }}
722 macro_rules! define_fn {($id:ident) => { fn $id{} }}
732 fn test_rename_for_param_inside() {
733 check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#);
737 fn test_rename_refs_for_fn_param() {
738 check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
742 fn test_rename_for_mut_param() {
743 check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
747 fn test_rename_struct_field() {
751 struct Foo { i$0: i32 }
754 fn new(i: i32) -> Self {
760 struct Foo { j: i32 }
763 fn new(i: i32) -> Self {
772 fn test_rename_struct_field_for_shorthand() {
773 mark::check!(test_rename_struct_field_for_shorthand);
777 struct Foo { i$0: i32 }
780 fn new(i: i32) -> Self {
786 struct Foo { j: i32 }
789 fn new(i: i32) -> Self {
798 fn test_rename_local_for_field_shorthand() {
799 mark::check!(test_rename_local_for_field_shorthand);
803 struct Foo { i: i32 }
806 fn new(i$0: i32) -> Self {
812 struct Foo { i: i32 }
815 fn new(j: i32) -> Self {
824 fn test_field_shorthand_correct_struct() {
828 struct Foo { i$0: i32 }
829 struct Bar { i: i32 }
832 fn new(i: i32) -> Self {
838 struct Foo { j: i32 }
839 struct Bar { i: i32 }
842 fn new(i: i32) -> Self {
851 fn test_shadow_local_for_struct_shorthand() {
855 struct Foo { i: i32 }
857 fn baz(i$0: i32) -> Self {
866 struct Foo { i: i32 }
868 fn baz(j: i32) -> Self {
869 let x = Foo { i: j };
880 fn test_rename_mod() {
917 dst: AnchoredPathBuf {
933 fn test_rename_mod_in_use_tree() {
943 pub struct FooContent;
946 use crate::foo$0::FooContent;
985 dst: AnchoredPathBuf {
1001 fn test_rename_mod_in_dir() {
1013 info: SourceChange {
1014 source_file_edits: [
1029 file_system_edits: [
1034 dst: AnchoredPathBuf {
1038 path: "../foo2/mod.rs",
1050 fn test_rename_unusually_nested_mod() {
1055 mod outer { mod fo$0o; }
1063 info: SourceChange {
1064 source_file_edits: [
1079 file_system_edits: [
1084 dst: AnchoredPathBuf {
1100 fn test_module_rename_in_path() {
1104 mod $0foo { pub fn bar() {} }
1106 fn main() { foo::bar(); }
1109 mod baz { pub fn bar() {} }
1111 fn main() { baz::bar(); }
1117 fn test_rename_mod_filename_and_path() {
1136 info: SourceChange {
1137 source_file_edits: [
1165 file_system_edits: [
1170 dst: AnchoredPathBuf {
1186 fn test_enum_variant_from_module_1() {
1191 pub enum Foo { Bar$0 }
1194 fn func(f: foo::Foo) {
1202 pub enum Foo { Baz }
1205 fn func(f: foo::Foo) {
1215 fn test_enum_variant_from_module_2() {
1220 pub struct Foo { pub bar$0: uint }
1223 fn foo(f: foo::Foo) {
1229 pub struct Foo { pub baz: uint }
1232 fn foo(f: foo::Foo) {
1240 fn test_parameter_to_self() {
1241 mark::check!(rename_to_self);
1245 struct Foo { i: i32 }
1248 fn f(foo$0: &mut Foo) -> i32 {
1254 struct Foo { i: i32 }
1257 fn f(&mut self) -> i32 {
1266 struct Foo { i: i32 }
1269 fn f(foo$0: Foo) -> i32 {
1275 struct Foo { i: i32 }
1287 fn test_parameter_to_self_error_no_impl() {
1291 struct Foo { i: i32 }
1293 fn f(foo$0: &mut Foo) -> i32 {
1297 "error: No impl block found for function",
1302 struct Foo { i: i32 }
1306 fn f(foo$0: &mut Foo) -> i32 {
1311 "error: Parameter type differs from impl block type",
1316 fn test_parameter_to_self_error_not_first() {
1320 struct Foo { i: i32 }
1322 fn f(x: (), foo$0: &mut Foo) -> i32 {
1327 "error: Only the first parameter can be self",
1332 fn test_parameter_to_self_impl_ref() {
1336 struct Foo { i: i32 }
1338 fn f(foo$0: &Foo) -> i32 {
1344 struct Foo { i: i32 }
1355 fn test_self_to_parameter() {
1359 struct Foo { i: i32 }
1362 fn f(&mut $0self) -> i32 {
1368 struct Foo { i: i32 }
1371 fn f(foo: &mut Foo) -> i32 {
1380 fn test_owned_self_to_parameter() {
1384 struct Foo { i: i32 }
1387 fn f($0self) -> i32 {
1393 struct Foo { i: i32 }
1396 fn f(foo: Foo) -> i32 {
1405 fn test_self_in_path_to_parameter() {
1409 struct Foo { i: i32 }
1412 fn f(&self) -> i32 {
1419 struct Foo { i: i32 }
1422 fn f(foo: &Foo) -> i32 {
1432 fn test_initializer_use_field_init_shorthand() {
1433 mark::check!(test_rename_field_expr_pat);
1437 struct Foo { i$0: i32 }
1439 fn foo(bar: i32) -> Foo {
1444 struct Foo { bar: i32 }
1446 fn foo(bar: i32) -> Foo {
1454 fn test_struct_field_destructure_into_shorthand() {
1458 struct Foo { i$0: i32 }
1461 let Foo { i: baz } = foo;
1466 struct Foo { baz: i32 }
1469 let Foo { baz } = foo;
1477 fn test_rename_binding_in_destructure_pat() {
1478 let expected_fixture = r#"
1484 let Foo { i: bar } = foo;
1496 let Foo { i: b } = foo;
1510 let Foo { i } = foo;
1519 fn test_rename_binding_in_destructure_param_pat() {
1527 fn foo(Foo { i }: foo) -> i32 {
1536 fn foo(Foo { i: bar }: foo) -> i32 {
1544 fn test_rename_lifetimes() {
1545 mark::check!(rename_lifetime);
1552 impl<'a> Foo<'a> for &'a () {
1553 fn foo() -> &'a$0 () {
1562 impl<'yeeee> Foo<'yeeee> for &'yeeee () {
1563 fn foo() -> &'yeeee () {
1572 fn test_rename_bind_pat() {
1577 enum CustomOption<T> {
1582 let test_variable = CustomOption::Some(22);
1584 match test_variable {
1585 CustomOption::Some(foo$0) if foo == 11 => {}
1591 enum CustomOption<T> {
1596 let test_variable = CustomOption::Some(22);
1598 match test_variable {
1599 CustomOption::Some(new_name) if new_name == 11 => {}
1607 fn test_rename_label() {
1611 fn foo<'a>() -> &'a () {
1620 fn foo<'a>() -> &'a () {
1632 fn test_self_to_self() {
1633 mark::check!(rename_self_to_self);