1 //! FIXME: write short doc here
2 use std::fmt::{self, Display};
5 use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics};
7 base_db::{AnchoredPathBuf, FileId, FileRange},
8 defs::{Definition, NameClass, NameRefClass},
12 use stdx::assert_never;
14 ast::{self, NameOwner},
15 lex_single_syntax_kind, AstNode, SyntaxKind, SyntaxNode, T,
18 use text_edit::TextEdit;
21 display::TryToNav, FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, SourceChange,
25 type RenameResult<T> = Result<T, RenameError>;
27 pub struct RenameError(pub(crate) String);
29 impl fmt::Display for RenameError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 Display::fmt(&self.0, f)
35 macro_rules! format_err {
36 ($fmt:expr) => {RenameError(format!($fmt))};
37 ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
41 ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))}
44 pub(crate) fn prepare_rename(
46 position: FilePosition,
47 ) -> RenameResult<RangeInfo<()>> {
48 let sema = Semantics::new(db);
49 let source_file = sema.parse(position.file_id);
50 let syntax = source_file.syntax();
51 let range = match &find_name_like(&sema, &syntax, position)
52 .ok_or_else(|| format_err!("No references found at position"))?
54 NameLike::Name(it) => it.syntax(),
55 NameLike::NameRef(it) => it.syntax(),
56 NameLike::Lifetime(it) => it.syntax(),
59 Ok(RangeInfo::new(range, ()))
64 position: FilePosition,
66 ) -> RenameResult<SourceChange> {
67 let sema = Semantics::new(db);
68 rename_with_semantics(&sema, position, new_name)
71 pub(crate) fn rename_with_semantics(
72 sema: &Semantics<RootDatabase>,
73 position: FilePosition,
75 ) -> RenameResult<SourceChange> {
76 let source_file = sema.parse(position.file_id);
77 let syntax = source_file.syntax();
79 let def = find_definition(sema, syntax, position)
80 .ok_or_else(|| format_err!("No references found at position"))?;
82 Definition::ModuleDef(ModuleDef::Module(module)) => rename_mod(&sema, module, new_name),
83 def => rename_reference(sema, def, new_name),
87 pub(crate) fn will_rename_file(
91 ) -> Option<SourceChange> {
92 let sema = Semantics::new(db);
93 let module = sema.to_module_def(file_id)?;
94 let mut change = rename_mod(&sema, module, new_name_stem).ok()?;
95 change.file_system_edits.clear();
99 #[derive(Copy, Clone, Debug, PartialEq)]
100 enum IdentifierKind {
107 fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
108 match lex_single_syntax_kind(new_name) {
109 Some(res) => match res {
110 (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
111 (T![_], _) => Ok(IdentifierKind::Underscore),
112 (T![self], _) => Ok(IdentifierKind::ToSelf),
113 (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
114 Ok(IdentifierKind::Lifetime)
116 (SyntaxKind::LIFETIME_IDENT, _) => {
117 bail!("Invalid name `{0}`: Cannot rename lifetime to {0}", new_name)
119 (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
120 (_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
122 None => bail!("Invalid name `{}`: not an identifier", new_name),
128 NameRef(ast::NameRef),
129 Lifetime(ast::Lifetime),
133 sema: &Semantics<RootDatabase>,
135 position: FilePosition,
136 ) -> Option<NameLike> {
137 let namelike = if let Some(name_ref) =
138 sema.find_node_at_offset_with_descend::<ast::NameRef>(syntax, position.offset)
140 NameLike::NameRef(name_ref)
141 } else if let Some(name) =
142 sema.find_node_at_offset_with_descend::<ast::Name>(syntax, position.offset)
145 } else if let Some(lifetime) =
146 sema.find_node_at_offset_with_descend::<ast::Lifetime>(syntax, position.offset)
148 NameLike::Lifetime(lifetime)
156 sema: &Semantics<RootDatabase>,
158 position: FilePosition,
159 ) -> Option<Definition> {
160 let def = match find_name_like(sema, syntax, position)? {
161 NameLike::Name(name) => NameClass::classify(sema, &name)?.referenced_or_defined(sema.db),
162 NameLike::NameRef(name_ref) => NameRefClass::classify(sema, &name_ref)?.referenced(sema.db),
163 NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime)
164 .map(|class| NameRefClass::referenced(class, sema.db))
166 NameClass::classify_lifetime(sema, &lifetime)
167 .map(|it| it.referenced_or_defined(sema.db))
173 fn source_edit_from_references(
174 sema: &Semantics<RootDatabase>,
176 references: &[FileReference],
178 ) -> (FileId, TextEdit) {
179 let mut edit = TextEdit::builder();
180 for reference in references {
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.range.start(), reference.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.range.end(), reference.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(
200 FileRange { file_id, range: reference.range },
205 replacement_text.push_str(new_name);
209 edit.replace(range, replacement_text);
211 (file_id, edit.finish())
214 fn edit_text_range_for_record_field_expr_or_pat(
215 sema: &Semantics<RootDatabase>,
216 file_range: FileRange,
219 let source_file = sema.parse(file_range.file_id);
220 let file_syntax = source_file.syntax();
221 let original_range = file_range.range;
223 syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
224 .and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
225 Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
229 syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
230 .and_then(|field_pat| match field_pat.pat() {
231 Some(ast::Pat::IdentPat(pat))
232 if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
234 Some(field_pat.syntax().text_range())
239 .unwrap_or(original_range)
243 sema: &Semantics<RootDatabase>,
246 ) -> RenameResult<SourceChange> {
247 if IdentifierKind::Ident != check_identifier(new_name)? {
248 bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
251 let mut source_change = SourceChange::default();
253 let InFile { file_id, value: def_source } = module.definition_source(sema.db);
254 let file_id = file_id.original_file(sema.db);
255 if let ModuleSource::SourceFile(..) = def_source {
256 // mod is defined in path/to/dir/mod.rs
257 let path = if module.is_mod_rs(sema.db) {
258 format!("../{}/mod.rs", new_name)
260 format!("{}.rs", new_name)
262 let dst = AnchoredPathBuf { anchor: file_id, path };
263 let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
264 source_change.push_file_system_edit(move_file);
267 if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) {
268 let file_id = file_id.original_file(sema.db);
269 match decl_source.name() {
270 Some(name) => source_change.insert_source_edit(
272 TextEdit::replace(name.syntax().text_range(), new_name.to_string()),
277 let def = Definition::ModuleDef(ModuleDef::Module(module));
278 let usages = def.usages(sema).all();
279 let ref_edits = usages.iter().map(|(&file_id, references)| {
280 source_edit_from_references(sema, file_id, references, new_name)
282 source_change.extend(ref_edits);
287 fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange> {
288 if assert_never!(local.is_self(sema.db)) {
289 bail!("rename_to_self invoked on self");
292 let fn_def = match local.parent(sema.db) {
293 hir::DefWithBody::Function(func) => func,
294 _ => bail!("Cannot rename non-param local to self"),
297 // FIXME: reimplement this on the hir instead
298 // as of the time of this writing params in hir don't keep their names
300 fn_def.source(sema.db).ok_or(format_err!("Cannot rename non-param local to self"))?.value;
302 let first_param_range = fn_ast
304 .and_then(|p| p.params().next())
305 .ok_or_else(|| format_err!("Method has no parameters"))?
308 let InFile { file_id, value: local_source } = local.source(sema.db);
310 either::Either::Left(pat)
311 if !first_param_range.contains_range(pat.syntax().text_range()) =>
313 bail!("Only the first parameter can be self");
318 let impl_block = fn_ast
321 .find_map(|node| ast::Impl::cast(node))
322 .and_then(|def| sema.to_def(&def))
323 .ok_or_else(|| format_err!("No impl block found for function"))?;
324 if fn_def.self_param(sema.db).is_some() {
325 bail!("Method already has a self parameter");
328 let params = fn_def.assoc_fn_params(sema.db);
329 let first_param = params.first().ok_or_else(|| format_err!("Method has no parameters"))?;
330 let first_param_ty = first_param.ty();
331 let impl_ty = impl_block.target_ty(sema.db);
332 let (ty, self_param) = if impl_ty.remove_ref().is_some() {
333 // if the impl is a ref to the type we can just match the `&T` with self directly
334 (first_param_ty.clone(), "self")
336 first_param_ty.remove_ref().map_or((first_param_ty.clone(), "self"), |ty| {
337 (ty, if first_param_ty.is_mutable_reference() { "&mut self" } else { "&self" })
342 bail!("Parameter type differs from impl block type");
345 let def = Definition::Local(local);
346 let usages = def.usages(sema).all();
347 let mut source_change = SourceChange::default();
348 source_change.extend(usages.iter().map(|(&file_id, references)| {
349 source_edit_from_references(sema, file_id, references, "self")
351 source_change.insert_source_edit(
352 file_id.original_file(sema.db),
353 TextEdit::replace(first_param_range, String::from(self_param)),
359 fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> 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 = self_param.syntax().ancestors().find_map(|it| ast::Impl::cast(it))?;
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>,
386 identifier_kind: IdentifierKind,
387 ) -> RenameResult<SourceChange> {
388 let (file_id, self_param) = match local.source(sema.db) {
389 InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param),
391 assert_never!(true, "rename_self_to_param invoked on a non-self local");
392 bail!("rename_self_to_param invoked on a non-self local");
396 let def = Definition::Local(local);
397 let usages = def.usages(sema).all();
398 let edit = text_edit_from_self_param(&self_param, new_name)
399 .ok_or_else(|| format_err!("No target type found"))?;
400 if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {
401 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
403 let mut source_change = SourceChange::default();
404 source_change.insert_source_edit(file_id.original_file(sema.db), edit);
405 source_change.extend(usages.iter().map(|(&file_id, references)| {
406 source_edit_from_references(sema, file_id, &references, new_name)
412 sema: &Semantics<RootDatabase>,
415 ) -> RenameResult<SourceChange> {
416 let ident_kind = check_identifier(new_name)?;
418 let def_is_lbl_or_lt = matches!(def,
419 Definition::GenericParam(hir::GenericParam::LifetimeParam(_))
420 | Definition::Label(_)
422 match (ident_kind, def) {
423 (IdentifierKind::ToSelf, _)
424 | (IdentifierKind::Underscore, _)
425 | (IdentifierKind::Ident, _)
426 if def_is_lbl_or_lt =>
428 mark::hit!(rename_not_a_lifetime_ident_ref);
429 bail!("Invalid name `{}`: not a lifetime identifier", new_name)
431 (IdentifierKind::Lifetime, _) if def_is_lbl_or_lt => mark::hit!(rename_lifetime),
432 (IdentifierKind::Lifetime, _) => {
433 mark::hit!(rename_not_an_ident_ref);
434 bail!("Invalid name `{}`: not an identifier", new_name)
436 (IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => {
438 mark::hit!(rename_self_to_self);
439 return Ok(SourceChange::default());
441 (ident_kind, Definition::Local(local)) if local.is_self(sema.db) => {
442 mark::hit!(rename_self_to_param);
443 return rename_self_to_param(sema, local, new_name, ident_kind);
445 (IdentifierKind::ToSelf, Definition::Local(local)) => {
446 mark::hit!(rename_to_self);
447 return rename_to_self(sema, local);
449 (IdentifierKind::ToSelf, _) => bail!("Invalid name `{}`: not an identifier", new_name),
450 (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
453 let usages = def.usages(sema).all();
454 if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
455 mark::hit!(rename_underscore_multiple);
456 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
458 let mut source_change = SourceChange::default();
459 source_change.extend(usages.iter().map(|(&file_id, references)| {
460 source_edit_from_references(sema, file_id, &references, new_name)
463 let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
464 source_change.insert_source_edit(file_id, edit);
468 fn source_edit_from_def(
469 sema: &Semantics<RootDatabase>,
472 ) -> RenameResult<(FileId, TextEdit)> {
473 let nav = def.try_to_nav(sema.db).unwrap();
475 let mut replacement_text = String::new();
476 let mut repl_range = nav.focus_or_full_range();
477 if let Definition::Local(local) = def {
478 if let Either::Left(pat) = local.source(sema.db).value {
480 pat.syntax().parent().and_then(ast::RecordPatField::cast),
481 Some(pat_field) if pat_field.name_ref().is_none()
483 replacement_text.push_str(": ");
484 replacement_text.push_str(new_name);
485 repl_range = TextRange::new(
486 pat.syntax().text_range().end(),
487 pat.syntax().text_range().end(),
492 if replacement_text.is_empty() {
493 replacement_text.push_str(new_name);
495 let edit = TextEdit::replace(repl_range, replacement_text);
496 Ok((nav.file_id, edit))
501 use expect_test::{expect, Expect};
502 use stdx::trim_indent;
503 use test_utils::{assert_eq_text, mark};
504 use text_edit::TextEdit;
506 use crate::{fixture, FileId};
508 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
509 let ra_fixture_after = &trim_indent(ra_fixture_after);
510 let (analysis, position) = fixture::position(ra_fixture_before);
511 let rename_result = analysis
512 .rename(position, new_name)
513 .unwrap_or_else(|err| panic!("Rename to '{}' was cancelled: {}", new_name, err));
514 match rename_result {
515 Ok(source_change) => {
516 let mut text_edit_builder = TextEdit::builder();
517 let mut file_id: Option<FileId> = None;
518 for edit in source_change.source_file_edits {
519 file_id = Some(edit.0);
520 for indel in edit.1.into_iter() {
521 text_edit_builder.replace(indel.delete, indel.insert);
524 if let Some(file_id) = file_id {
525 let mut result = analysis.file_text(file_id).unwrap().to_string();
526 text_edit_builder.finish().apply(&mut result);
527 assert_eq_text!(ra_fixture_after, &*result);
531 if ra_fixture_after.starts_with("error:") {
532 let error_message = ra_fixture_after
535 .skip("error:".len())
536 .collect::<String>();
537 assert_eq!(error_message.trim(), err.to_string());
540 panic!("Rename to '{}' failed unexpectedly: {}", new_name, err)
546 fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
547 let (analysis, position) = fixture::position(ra_fixture);
548 let source_change = analysis
549 .rename(position, new_name)
551 .expect("Expect returned RangeInfo to be Some, but was None");
552 expect.assert_debug_eq(&source_change)
556 fn test_rename_to_underscore() {
557 check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#);
561 fn test_rename_to_raw_identifier() {
562 check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
566 fn test_rename_to_invalid_identifier1() {
569 r#"fn main() { let i$0 = 1; }"#,
570 "error: Invalid name `invalid!`: not an identifier",
575 fn test_rename_to_invalid_identifier2() {
578 r#"fn main() { let i$0 = 1; }"#,
579 "error: Invalid name `multiple tokens`: not an identifier",
584 fn test_rename_to_invalid_identifier3() {
587 r#"fn main() { let i$0 = 1; }"#,
588 "error: Invalid name `let`: not an identifier",
593 fn test_rename_to_invalid_identifier_lifetime() {
594 mark::check!(rename_not_an_ident_ref);
597 r#"fn main() { let i$0 = 1; }"#,
598 "error: Invalid name `'foo`: not an identifier",
603 fn test_rename_to_invalid_identifier_lifetime2() {
604 mark::check!(rename_not_a_lifetime_ident_ref);
607 r#"fn main<'a>(_: &'a$0 ()) {}"#,
608 "error: Invalid name `foo`: not a lifetime identifier",
613 fn test_rename_to_underscore_invalid() {
614 mark::check!(rename_underscore_multiple);
617 r#"fn main(foo$0: ()) {foo;}"#,
618 "error: Cannot rename reference to `_` as it is being referenced multiple times",
623 fn test_rename_mod_invalid() {
627 "error: Invalid name `'foo`: cannot rename module to 'foo",
632 fn test_rename_for_local() {
633 mark::check!(rename_ident);
662 fn test_rename_unresolved_reference() {
665 r#"fn main() { let _ = unresolved_ref$0; }"#,
666 "error: No references found at position",
671 fn test_rename_for_macro_args() {
675 macro_rules! foo {($i:ident) => {$i} }
682 macro_rules! foo {($i:ident) => {$i} }
692 fn test_rename_for_macro_args_rev() {
696 macro_rules! foo {($i:ident) => {$i} }
703 macro_rules! foo {($i:ident) => {$i} }
713 fn test_rename_for_macro_define_fn() {
717 macro_rules! define_fn {($id:ident) => { fn $id{} }}
724 macro_rules! define_fn {($id:ident) => { fn $id{} }}
734 fn test_rename_for_macro_define_fn_rev() {
738 macro_rules! define_fn {($id:ident) => { fn $id{} }}
745 macro_rules! define_fn {($id:ident) => { fn $id{} }}
755 fn test_rename_for_param_inside() {
756 check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#);
760 fn test_rename_refs_for_fn_param() {
761 check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
765 fn test_rename_for_mut_param() {
766 check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
770 fn test_rename_struct_field() {
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_struct_field_for_shorthand() {
796 mark::check!(test_rename_struct_field_for_shorthand);
800 struct Foo { i$0: i32 }
803 fn new(i: i32) -> Self {
809 struct Foo { j: i32 }
812 fn new(i: i32) -> Self {
821 fn test_rename_local_for_field_shorthand() {
822 mark::check!(test_rename_local_for_field_shorthand);
826 struct Foo { i: i32 }
829 fn new(i$0: i32) -> Self {
835 struct Foo { i: i32 }
838 fn new(j: i32) -> Self {
847 fn test_field_shorthand_correct_struct() {
851 struct Foo { i$0: i32 }
852 struct Bar { i: i32 }
855 fn new(i: i32) -> Self {
861 struct Foo { j: i32 }
862 struct Bar { i: i32 }
865 fn new(i: i32) -> Self {
874 fn test_shadow_local_for_struct_shorthand() {
878 struct Foo { i: i32 }
880 fn baz(i$0: i32) -> Self {
889 struct Foo { i: i32 }
891 fn baz(j: i32) -> Self {
892 let x = Foo { i: j };
903 fn test_rename_mod() {
935 dst: AnchoredPathBuf {
950 fn test_rename_mod_in_use_tree() {
960 pub struct FooContent;
963 use crate::foo$0::FooContent;
994 dst: AnchoredPathBuf {
1009 fn test_rename_mod_in_dir() {
1020 source_file_edits: {
1032 file_system_edits: [
1037 dst: AnchoredPathBuf {
1041 path: "../foo2/mod.rs",
1052 fn test_rename_unusually_nested_mod() {
1057 mod outer { mod fo$0o; }
1064 source_file_edits: {
1076 file_system_edits: [
1081 dst: AnchoredPathBuf {
1096 fn test_module_rename_in_path() {
1100 mod $0foo { pub fn bar() {} }
1102 fn main() { foo::bar(); }
1105 mod baz { pub fn bar() {} }
1107 fn main() { baz::bar(); }
1113 fn test_rename_mod_filename_and_path() {
1131 source_file_edits: {
1153 file_system_edits: [
1158 dst: AnchoredPathBuf {
1173 fn test_enum_variant_from_module_1() {
1178 pub enum Foo { Bar$0 }
1181 fn func(f: foo::Foo) {
1189 pub enum Foo { Baz }
1192 fn func(f: foo::Foo) {
1202 fn test_enum_variant_from_module_2() {
1207 pub struct Foo { pub bar$0: uint }
1210 fn foo(f: foo::Foo) {
1216 pub struct Foo { pub baz: uint }
1219 fn foo(f: foo::Foo) {
1227 fn test_parameter_to_self() {
1228 mark::check!(rename_to_self);
1232 struct Foo { i: i32 }
1235 fn f(foo$0: &mut Foo) -> i32 {
1241 struct Foo { i: i32 }
1244 fn f(&mut self) -> i32 {
1253 struct Foo { i: i32 }
1256 fn f(foo$0: Foo) -> i32 {
1262 struct Foo { i: i32 }
1274 fn test_parameter_to_self_error_no_impl() {
1278 struct Foo { i: i32 }
1280 fn f(foo$0: &mut Foo) -> i32 {
1284 "error: No impl block found for function",
1289 struct Foo { i: i32 }
1293 fn f(foo$0: &mut Foo) -> i32 {
1298 "error: Parameter type differs from impl block type",
1303 fn test_parameter_to_self_error_not_first() {
1307 struct Foo { i: i32 }
1309 fn f(x: (), foo$0: &mut Foo) -> i32 {
1314 "error: Only the first parameter can be self",
1319 fn test_parameter_to_self_impl_ref() {
1323 struct Foo { i: i32 }
1325 fn f(foo$0: &Foo) -> i32 {
1331 struct Foo { i: i32 }
1342 fn test_self_to_parameter() {
1346 struct Foo { i: i32 }
1349 fn f(&mut $0self) -> i32 {
1355 struct Foo { i: i32 }
1358 fn f(foo: &mut Foo) -> i32 {
1367 fn test_owned_self_to_parameter() {
1368 mark::check!(rename_self_to_param);
1372 struct Foo { i: i32 }
1375 fn f($0self) -> i32 {
1381 struct Foo { i: i32 }
1384 fn f(foo: Foo) -> i32 {
1393 fn test_self_in_path_to_parameter() {
1397 struct Foo { i: i32 }
1400 fn f(&self) -> i32 {
1407 struct Foo { i: i32 }
1410 fn f(foo: &Foo) -> i32 {
1420 fn test_initializer_use_field_init_shorthand() {
1421 mark::check!(test_rename_field_expr_pat);
1425 struct Foo { i$0: i32 }
1427 fn foo(bar: i32) -> Foo {
1432 struct Foo { bar: i32 }
1434 fn foo(bar: i32) -> Foo {
1442 fn test_struct_field_destructure_into_shorthand() {
1446 struct Foo { i$0: i32 }
1449 let Foo { i: baz } = foo;
1454 struct Foo { baz: i32 }
1457 let Foo { baz } = foo;
1465 fn test_rename_binding_in_destructure_pat() {
1466 let expected_fixture = r#"
1472 let Foo { i: bar } = foo;
1484 let Foo { i: b } = foo;
1498 let Foo { i } = foo;
1507 fn test_rename_binding_in_destructure_param_pat() {
1515 fn foo(Foo { i }: foo) -> i32 {
1524 fn foo(Foo { i: bar }: foo) -> i32 {
1532 fn test_rename_lifetimes() {
1533 mark::check!(rename_lifetime);
1540 impl<'a> Foo<'a> for &'a () {
1541 fn foo() -> &'a$0 () {
1550 impl<'yeeee> Foo<'yeeee> for &'yeeee () {
1551 fn foo() -> &'yeeee () {
1560 fn test_rename_bind_pat() {
1565 enum CustomOption<T> {
1570 let test_variable = CustomOption::Some(22);
1572 match test_variable {
1573 CustomOption::Some(foo$0) if foo == 11 => {}
1579 enum CustomOption<T> {
1584 let test_variable = CustomOption::Some(22);
1586 match test_variable {
1587 CustomOption::Some(new_name) if new_name == 11 => {}
1595 fn test_rename_label() {
1599 fn foo<'a>() -> &'a () {
1608 fn foo<'a>() -> &'a () {
1620 fn test_self_to_self() {
1621 mark::check!(rename_self_to_self);