1 //! Renaming functionality.
2 //!
3 //! This is mostly front-end for [`ide_db::rename`], but it also includes the
4 //! tests. This module also implements a couple of magic tricks, like renaming
5 //! `self` and to `self` (to switch between associated function and method).
6
7 use hir::{AsAssocItem, InFile, Semantics};
8 use ide_db::{
9 base_db::FileId,
10 defs::{Definition, NameClass, NameRefClass},
11 rename::{bail, format_err, source_edit_from_references, IdentifierKind},
12 RootDatabase,
13 };
14 use itertools::Itertools;
15 use stdx::{always, never};
16 use syntax::{ast, AstNode, SyntaxNode};
17
18 use text_edit::TextEdit;
19
20 use crate::{FilePosition, RangeInfo, SourceChange};
21
22 pub use ide_db::rename::RenameError;
23
24 type RenameResult<T> = Result<T, RenameError>;
25
26 /// Prepares a rename. The sole job of this function is to return the TextRange of the thing that is
27 /// being targeted for a rename.
prepare_rename( db: &RootDatabase, position: FilePosition, ) -> RenameResult<RangeInfo<()>>28 pub(crate) fn prepare_rename(
29 db: &RootDatabase,
30 position: FilePosition,
31 ) -> RenameResult<RangeInfo<()>> {
32 let sema = Semantics::new(db);
33 let source_file = sema.parse(position.file_id);
34 let syntax = source_file.syntax();
35
36 let res = find_definitions(&sema, syntax, position)?
37 .map(|(name_like, def)| {
38 // ensure all ranges are valid
39
40 if def.range_for_rename(&sema).is_none() {
41 bail!("No references found at position")
42 }
43 let frange = sema.original_range(name_like.syntax());
44
45 always!(
46 frange.range.contains_inclusive(position.offset)
47 && frange.file_id == position.file_id
48 );
49 Ok(frange.range)
50 })
51 .reduce(|acc, cur| match (acc, cur) {
52 // ensure all ranges are the same
53 (Ok(acc_inner), Ok(cur_inner)) if acc_inner == cur_inner => Ok(acc_inner),
54 (Err(e), _) => Err(e),
55 _ => bail!("inconsistent text range"),
56 });
57
58 match res {
59 // ensure at least one definition was found
60 Some(res) => res.map(|range| RangeInfo::new(range, ())),
61 None => bail!("No references found at position"),
62 }
63 }
64
65 // Feature: Rename
66 //
67 // Renames the item below the cursor and all of its references
68 //
69 // |===
70 // | Editor | Shortcut
71 //
72 // | VS Code | kbd:[F2]
73 // |===
74 //
75 // image::https://user-images.githubusercontent.com/48062697/113065582-055aae80-91b1-11eb-8ade-2b58e6d81883.gif[]
rename( db: &RootDatabase, position: FilePosition, new_name: &str, ) -> RenameResult<SourceChange>76 pub(crate) fn rename(
77 db: &RootDatabase,
78 position: FilePosition,
79 new_name: &str,
80 ) -> RenameResult<SourceChange> {
81 let sema = Semantics::new(db);
82 let source_file = sema.parse(position.file_id);
83 let syntax = source_file.syntax();
84
85 let defs = find_definitions(&sema, syntax, position)?;
86
87 let ops: RenameResult<Vec<SourceChange>> = defs
88 .map(|(_namelike, def)| {
89 if let Definition::Local(local) = def {
90 if let Some(self_param) = local.as_self_param(sema.db) {
91 cov_mark::hit!(rename_self_to_param);
92 return rename_self_to_param(&sema, local, self_param, new_name);
93 }
94 if new_name == "self" {
95 cov_mark::hit!(rename_to_self);
96 return rename_to_self(&sema, local);
97 }
98 }
99 def.rename(&sema, new_name)
100 })
101 .collect();
102
103 ops?.into_iter()
104 .reduce(|acc, elem| acc.merge(elem))
105 .ok_or_else(|| format_err!("No references found at position"))
106 }
107
108 /// Called by the client when it is about to rename a file.
will_rename_file( db: &RootDatabase, file_id: FileId, new_name_stem: &str, ) -> Option<SourceChange>109 pub(crate) fn will_rename_file(
110 db: &RootDatabase,
111 file_id: FileId,
112 new_name_stem: &str,
113 ) -> Option<SourceChange> {
114 let sema = Semantics::new(db);
115 let module = sema.to_module_def(file_id)?;
116 let def = Definition::Module(module);
117 let mut change = def.rename(&sema, new_name_stem).ok()?;
118 change.file_system_edits.clear();
119 Some(change)
120 }
121
find_definitions( sema: &Semantics<RootDatabase>, syntax: &SyntaxNode, position: FilePosition, ) -> RenameResult<impl Iterator<Item = (ast::NameLike, Definition)>>122 fn find_definitions(
123 sema: &Semantics<RootDatabase>,
124 syntax: &SyntaxNode,
125 position: FilePosition,
126 ) -> RenameResult<impl Iterator<Item = (ast::NameLike, Definition)>> {
127 let symbols = sema
128 .find_nodes_at_offset_with_descend::<ast::NameLike>(syntax, position.offset)
129 .map(|name_like| {
130 let res = match &name_like {
131 // renaming aliases would rename the item being aliased as the HIR doesn't track aliases yet
132 ast::NameLike::Name(name)
133 if name
134 .syntax()
135 .parent()
136 .map_or(false, |it| ast::Rename::can_cast(it.kind())) =>
137 {
138 bail!("Renaming aliases is currently unsupported")
139 }
140 ast::NameLike::Name(name) => NameClass::classify(sema, name)
141 .map(|class| match class {
142 NameClass::Definition(it) | NameClass::ConstReference(it) => it,
143 NameClass::PatFieldShorthand { local_def, field_ref: _ } => {
144 Definition::Local(local_def)
145 }
146 })
147 .map(|def| (name_like.clone(), def))
148 .ok_or_else(|| format_err!("No references found at position")),
149 ast::NameLike::NameRef(name_ref) => {
150 NameRefClass::classify(sema, name_ref)
151 .map(|class| match class {
152 NameRefClass::Definition(def) => def,
153 NameRefClass::FieldShorthand { local_ref, field_ref: _ } => {
154 Definition::Local(local_ref)
155 }
156 })
157 .ok_or_else(|| format_err!("No references found at position"))
158 .and_then(|def| {
159 // if the name differs from the definitions name it has to be an alias
160 if def
161 .name(sema.db)
162 .map_or(false, |it| it.to_smol_str() != name_ref.text().as_str())
163 {
164 Err(format_err!("Renaming aliases is currently unsupported"))
165 } else {
166 Ok((name_like.clone(), def))
167 }
168 })
169 }
170 ast::NameLike::Lifetime(lifetime) => {
171 NameRefClass::classify_lifetime(sema, lifetime)
172 .and_then(|class| match class {
173 NameRefClass::Definition(def) => Some(def),
174 _ => None,
175 })
176 .or_else(|| {
177 NameClass::classify_lifetime(sema, lifetime).and_then(|it| match it {
178 NameClass::Definition(it) => Some(it),
179 _ => None,
180 })
181 })
182 .map(|def| (name_like, def))
183 .ok_or_else(|| format_err!("No references found at position"))
184 }
185 };
186 res
187 });
188
189 let res: RenameResult<Vec<_>> = symbols.collect();
190 match res {
191 Ok(v) => {
192 if v.is_empty() {
193 // FIXME: some semantic duplication between "empty vec" and "Err()"
194 Err(format_err!("No references found at position"))
195 } else {
196 // remove duplicates, comparing `Definition`s
197 Ok(v.into_iter().unique_by(|t| t.1))
198 }
199 }
200 Err(e) => Err(e),
201 }
202 }
203
rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange>204 fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange> {
205 if never!(local.is_self(sema.db)) {
206 bail!("rename_to_self invoked on self");
207 }
208
209 let fn_def = match local.parent(sema.db) {
210 hir::DefWithBody::Function(func) => func,
211 _ => bail!("Cannot rename local to self outside of function"),
212 };
213
214 if fn_def.self_param(sema.db).is_some() {
215 bail!("Method already has a self parameter");
216 }
217
218 let params = fn_def.assoc_fn_params(sema.db);
219 let first_param = params
220 .first()
221 .ok_or_else(|| format_err!("Cannot rename local to self unless it is a parameter"))?;
222 if first_param.as_local(sema.db) != local {
223 bail!("Only the first parameter may be renamed to self");
224 }
225
226 let assoc_item = fn_def
227 .as_assoc_item(sema.db)
228 .ok_or_else(|| format_err!("Cannot rename parameter to self for free function"))?;
229 let impl_ = match assoc_item.container(sema.db) {
230 hir::AssocItemContainer::Trait(_) => {
231 bail!("Cannot rename parameter to self for trait functions");
232 }
233 hir::AssocItemContainer::Impl(impl_) => impl_,
234 };
235 let first_param_ty = first_param.ty();
236 let impl_ty = impl_.self_ty(sema.db);
237 let (ty, self_param) = if impl_ty.remove_ref().is_some() {
238 // if the impl is a ref to the type we can just match the `&T` with self directly
239 (first_param_ty.clone(), "self")
240 } else {
241 first_param_ty.remove_ref().map_or((first_param_ty.clone(), "self"), |ty| {
242 (ty, if first_param_ty.is_mutable_reference() { "&mut self" } else { "&self" })
243 })
244 };
245
246 if ty != impl_ty {
247 bail!("Parameter type differs from impl block type");
248 }
249
250 let InFile { file_id, value: param_source } =
251 first_param.source(sema.db).ok_or_else(|| format_err!("No source for parameter found"))?;
252
253 let def = Definition::Local(local);
254 let usages = def.usages(sema).all();
255 let mut source_change = SourceChange::default();
256 source_change.extend(usages.iter().map(|(&file_id, references)| {
257 (file_id, source_edit_from_references(references, def, "self"))
258 }));
259 source_change.insert_source_edit(
260 file_id.original_file(sema.db),
261 TextEdit::replace(param_source.syntax().text_range(), String::from(self_param)),
262 );
263 Ok(source_change)
264 }
265
rename_self_to_param( sema: &Semantics<RootDatabase>, local: hir::Local, self_param: hir::SelfParam, new_name: &str, ) -> RenameResult<SourceChange>266 fn rename_self_to_param(
267 sema: &Semantics<RootDatabase>,
268 local: hir::Local,
269 self_param: hir::SelfParam,
270 new_name: &str,
271 ) -> RenameResult<SourceChange> {
272 if new_name == "self" {
273 // Let's do nothing rather than complain.
274 cov_mark::hit!(rename_self_to_self);
275 return Ok(SourceChange::default());
276 }
277
278 let identifier_kind = IdentifierKind::classify(new_name)?;
279
280 let InFile { file_id, value: self_param } =
281 self_param.source(sema.db).ok_or_else(|| format_err!("cannot find function source"))?;
282
283 let def = Definition::Local(local);
284 let usages = def.usages(sema).all();
285 let edit = text_edit_from_self_param(&self_param, new_name)
286 .ok_or_else(|| format_err!("No target type found"))?;
287 if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {
288 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
289 }
290 let mut source_change = SourceChange::default();
291 source_change.insert_source_edit(file_id.original_file(sema.db), edit);
292 source_change.extend(usages.iter().map(|(&file_id, references)| {
293 (file_id, source_edit_from_references(references, def, new_name))
294 }));
295 Ok(source_change)
296 }
297
text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit>298 fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit> {
299 fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
300 if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
301 return Some(p.path()?.segment()?.name_ref()?.text().to_string());
302 }
303 None
304 }
305
306 let impl_def = self_param.syntax().ancestors().find_map(ast::Impl::cast)?;
307 let type_name = target_type_name(&impl_def)?;
308
309 let mut replacement_text = String::from(new_name);
310 replacement_text.push_str(": ");
311 match (self_param.amp_token(), self_param.mut_token()) {
312 (Some(_), None) => replacement_text.push('&'),
313 (Some(_), Some(_)) => replacement_text.push_str("&mut "),
314 (_, _) => (),
315 };
316 replacement_text.push_str(type_name.as_str());
317
318 Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
319 }
320
321 #[cfg(test)]
322 mod tests {
323 use expect_test::{expect, Expect};
324 use stdx::trim_indent;
325 use test_utils::assert_eq_text;
326 use text_edit::TextEdit;
327
328 use crate::{fixture, FileId};
329
330 use super::{RangeInfo, RenameError};
331
332 #[track_caller]
check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str)333 fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
334 let ra_fixture_after = &trim_indent(ra_fixture_after);
335 let (analysis, position) = fixture::position(ra_fixture_before);
336 let rename_result = analysis
337 .rename(position, new_name)
338 .unwrap_or_else(|err| panic!("Rename to '{}' was cancelled: {}", new_name, err));
339 match rename_result {
340 Ok(source_change) => {
341 let mut text_edit_builder = TextEdit::builder();
342 let mut file_id: Option<FileId> = None;
343 for edit in source_change.source_file_edits {
344 file_id = Some(edit.0);
345 for indel in edit.1.into_iter() {
346 text_edit_builder.replace(indel.delete, indel.insert);
347 }
348 }
349 if let Some(file_id) = file_id {
350 let mut result = analysis.file_text(file_id).unwrap().to_string();
351 text_edit_builder.finish().apply(&mut result);
352 assert_eq_text!(ra_fixture_after, &*result);
353 }
354 }
355 Err(err) => {
356 if ra_fixture_after.starts_with("error:") {
357 let error_message = ra_fixture_after
358 .chars()
359 .into_iter()
360 .skip("error:".len())
361 .collect::<String>();
362 assert_eq!(error_message.trim(), err.to_string());
363 } else {
364 panic!("Rename to '{}' failed unexpectedly: {}", new_name, err)
365 }
366 }
367 };
368 }
369
check_expect(new_name: &str, ra_fixture: &str, expect: Expect)370 fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
371 let (analysis, position) = fixture::position(ra_fixture);
372 let source_change =
373 analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError");
374 expect.assert_debug_eq(&source_change)
375 }
376
check_prepare(ra_fixture: &str, expect: Expect)377 fn check_prepare(ra_fixture: &str, expect: Expect) {
378 let (analysis, position) = fixture::position(ra_fixture);
379 let result = analysis
380 .prepare_rename(position)
381 .unwrap_or_else(|err| panic!("PrepareRename was cancelled: {}", err));
382 match result {
383 Ok(RangeInfo { range, info: () }) => {
384 let source = analysis.file_text(position.file_id).unwrap();
385 expect.assert_eq(&format!("{:?}: {}", range, &source[range]))
386 }
387 Err(RenameError(err)) => expect.assert_eq(&err),
388 };
389 }
390
391 #[test]
test_prepare_rename_namelikes()392 fn test_prepare_rename_namelikes() {
393 check_prepare(r"fn name$0<'lifetime>() {}", expect![[r#"3..7: name"#]]);
394 check_prepare(r"fn name<'lifetime$0>() {}", expect![[r#"8..17: 'lifetime"#]]);
395 check_prepare(r"fn name<'lifetime>() { name$0(); }", expect![[r#"23..27: name"#]]);
396 }
397
398 #[test]
test_prepare_rename_in_macro()399 fn test_prepare_rename_in_macro() {
400 check_prepare(
401 r"macro_rules! foo {
402 ($ident:ident) => {
403 pub struct $ident;
404 }
405 }
406 foo!(Foo$0);",
407 expect![[r#"83..86: Foo"#]],
408 );
409 }
410
411 #[test]
test_prepare_rename_keyword()412 fn test_prepare_rename_keyword() {
413 check_prepare(r"struct$0 Foo;", expect![[r#"No references found at position"#]]);
414 }
415
416 #[test]
test_prepare_rename_tuple_field()417 fn test_prepare_rename_tuple_field() {
418 check_prepare(
419 r#"
420 struct Foo(i32);
421
422 fn baz() {
423 let mut x = Foo(4);
424 x.0$0 = 5;
425 }
426 "#,
427 expect![[r#"No references found at position"#]],
428 );
429 }
430
431 #[test]
test_prepare_rename_builtin()432 fn test_prepare_rename_builtin() {
433 check_prepare(
434 r#"
435 fn foo() {
436 let x: i32$0 = 0;
437 }
438 "#,
439 expect![[r#"No references found at position"#]],
440 );
441 }
442
443 #[test]
test_prepare_rename_self()444 fn test_prepare_rename_self() {
445 check_prepare(
446 r#"
447 struct Foo {}
448
449 impl Foo {
450 fn foo(self) -> Self$0 {
451 self
452 }
453 }
454 "#,
455 expect![[r#"No references found at position"#]],
456 );
457 }
458
459 #[test]
test_rename_to_underscore()460 fn test_rename_to_underscore() {
461 check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#);
462 }
463
464 #[test]
test_rename_to_raw_identifier()465 fn test_rename_to_raw_identifier() {
466 check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
467 }
468
469 #[test]
test_rename_to_invalid_identifier1()470 fn test_rename_to_invalid_identifier1() {
471 check(
472 "invalid!",
473 r#"fn main() { let i$0 = 1; }"#,
474 "error: Invalid name `invalid!`: not an identifier",
475 );
476 }
477
478 #[test]
test_rename_to_invalid_identifier2()479 fn test_rename_to_invalid_identifier2() {
480 check(
481 "multiple tokens",
482 r#"fn main() { let i$0 = 1; }"#,
483 "error: Invalid name `multiple tokens`: not an identifier",
484 );
485 }
486
487 #[test]
test_rename_to_invalid_identifier3()488 fn test_rename_to_invalid_identifier3() {
489 check(
490 "let",
491 r#"fn main() { let i$0 = 1; }"#,
492 "error: Invalid name `let`: not an identifier",
493 );
494 }
495
496 #[test]
test_rename_to_invalid_identifier_lifetime()497 fn test_rename_to_invalid_identifier_lifetime() {
498 cov_mark::check!(rename_not_an_ident_ref);
499 check(
500 "'foo",
501 r#"fn main() { let i$0 = 1; }"#,
502 "error: Invalid name `'foo`: not an identifier",
503 );
504 }
505
506 #[test]
test_rename_to_invalid_identifier_lifetime2()507 fn test_rename_to_invalid_identifier_lifetime2() {
508 cov_mark::check!(rename_not_a_lifetime_ident_ref);
509 check(
510 "foo",
511 r#"fn main<'a>(_: &'a$0 ()) {}"#,
512 "error: Invalid name `foo`: not a lifetime identifier",
513 );
514 }
515
516 #[test]
test_rename_to_underscore_invalid()517 fn test_rename_to_underscore_invalid() {
518 cov_mark::check!(rename_underscore_multiple);
519 check(
520 "_",
521 r#"fn main(foo$0: ()) {foo;}"#,
522 "error: Cannot rename reference to `_` as it is being referenced multiple times",
523 );
524 }
525
526 #[test]
test_rename_mod_invalid()527 fn test_rename_mod_invalid() {
528 check(
529 "'foo",
530 r#"mod foo$0 {}"#,
531 "error: Invalid name `'foo`: cannot rename module to 'foo",
532 );
533 }
534
535 #[test]
test_rename_for_local()536 fn test_rename_for_local() {
537 check(
538 "k",
539 r#"
540 fn main() {
541 let mut i = 1;
542 let j = 1;
543 i = i$0 + j;
544
545 { i = 0; }
546
547 i = 5;
548 }
549 "#,
550 r#"
551 fn main() {
552 let mut k = 1;
553 let j = 1;
554 k = k + j;
555
556 { k = 0; }
557
558 k = 5;
559 }
560 "#,
561 );
562 }
563
564 #[test]
test_rename_unresolved_reference()565 fn test_rename_unresolved_reference() {
566 check(
567 "new_name",
568 r#"fn main() { let _ = unresolved_ref$0; }"#,
569 "error: No references found at position",
570 );
571 }
572
573 #[test]
test_rename_macro_multiple_occurrences()574 fn test_rename_macro_multiple_occurrences() {
575 check(
576 "Baaah",
577 r#"macro_rules! foo {
578 ($ident:ident) => {
579 const $ident: () = ();
580 struct $ident {}
581 };
582 }
583
584 foo!($0Foo);
585 const _: () = Foo;
586 const _: Foo = Foo {};
587 "#,
588 r#"
589 macro_rules! foo {
590 ($ident:ident) => {
591 const $ident: () = ();
592 struct $ident {}
593 };
594 }
595
596 foo!(Baaah);
597 const _: () = Baaah;
598 const _: Baaah = Baaah {};
599 "#,
600 )
601 }
602
603 #[test]
test_rename_for_macro_args()604 fn test_rename_for_macro_args() {
605 check(
606 "b",
607 r#"
608 macro_rules! foo {($i:ident) => {$i} }
609 fn main() {
610 let a$0 = "test";
611 foo!(a);
612 }
613 "#,
614 r#"
615 macro_rules! foo {($i:ident) => {$i} }
616 fn main() {
617 let b = "test";
618 foo!(b);
619 }
620 "#,
621 );
622 }
623
624 #[test]
test_rename_for_macro_args_rev()625 fn test_rename_for_macro_args_rev() {
626 check(
627 "b",
628 r#"
629 macro_rules! foo {($i:ident) => {$i} }
630 fn main() {
631 let a = "test";
632 foo!(a$0);
633 }
634 "#,
635 r#"
636 macro_rules! foo {($i:ident) => {$i} }
637 fn main() {
638 let b = "test";
639 foo!(b);
640 }
641 "#,
642 );
643 }
644
645 #[test]
test_rename_for_macro_define_fn()646 fn test_rename_for_macro_define_fn() {
647 check(
648 "bar",
649 r#"
650 macro_rules! define_fn {($id:ident) => { fn $id{} }}
651 define_fn!(foo);
652 fn main() {
653 fo$0o();
654 }
655 "#,
656 r#"
657 macro_rules! define_fn {($id:ident) => { fn $id{} }}
658 define_fn!(bar);
659 fn main() {
660 bar();
661 }
662 "#,
663 );
664 }
665
666 #[test]
test_rename_for_macro_define_fn_rev()667 fn test_rename_for_macro_define_fn_rev() {
668 check(
669 "bar",
670 r#"
671 macro_rules! define_fn {($id:ident) => { fn $id{} }}
672 define_fn!(fo$0o);
673 fn main() {
674 foo();
675 }
676 "#,
677 r#"
678 macro_rules! define_fn {($id:ident) => { fn $id{} }}
679 define_fn!(bar);
680 fn main() {
681 bar();
682 }
683 "#,
684 );
685 }
686
687 #[test]
test_rename_for_param_inside()688 fn test_rename_for_param_inside() {
689 check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#);
690 }
691
692 #[test]
test_rename_refs_for_fn_param()693 fn test_rename_refs_for_fn_param() {
694 check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
695 }
696
697 #[test]
test_rename_for_mut_param()698 fn test_rename_for_mut_param() {
699 check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
700 }
701
702 #[test]
test_rename_struct_field()703 fn test_rename_struct_field() {
704 check(
705 "foo",
706 r#"
707 struct Foo { field$0: i32 }
708
709 impl Foo {
710 fn new(i: i32) -> Self {
711 Self { field: i }
712 }
713 }
714 "#,
715 r#"
716 struct Foo { foo: i32 }
717
718 impl Foo {
719 fn new(i: i32) -> Self {
720 Self { foo: i }
721 }
722 }
723 "#,
724 );
725 }
726
727 #[test]
test_rename_field_in_field_shorthand()728 fn test_rename_field_in_field_shorthand() {
729 cov_mark::check!(test_rename_field_in_field_shorthand);
730 check(
731 "field",
732 r#"
733 struct Foo { foo$0: i32 }
734
735 impl Foo {
736 fn new(foo: i32) -> Self {
737 Self { foo }
738 }
739 }
740 "#,
741 r#"
742 struct Foo { field: i32 }
743
744 impl Foo {
745 fn new(foo: i32) -> Self {
746 Self { field: foo }
747 }
748 }
749 "#,
750 );
751 }
752
753 #[test]
test_rename_local_in_field_shorthand()754 fn test_rename_local_in_field_shorthand() {
755 cov_mark::check!(test_rename_local_in_field_shorthand);
756 check(
757 "j",
758 r#"
759 struct Foo { i: i32 }
760
761 impl Foo {
762 fn new(i$0: i32) -> Self {
763 Self { i }
764 }
765 }
766 "#,
767 r#"
768 struct Foo { i: i32 }
769
770 impl Foo {
771 fn new(j: i32) -> Self {
772 Self { i: j }
773 }
774 }
775 "#,
776 );
777 }
778
779 #[test]
test_field_shorthand_correct_struct()780 fn test_field_shorthand_correct_struct() {
781 check(
782 "j",
783 r#"
784 struct Foo { i$0: i32 }
785 struct Bar { i: i32 }
786
787 impl Bar {
788 fn new(i: i32) -> Self {
789 Self { i }
790 }
791 }
792 "#,
793 r#"
794 struct Foo { j: i32 }
795 struct Bar { i: i32 }
796
797 impl Bar {
798 fn new(i: i32) -> Self {
799 Self { i }
800 }
801 }
802 "#,
803 );
804 }
805
806 #[test]
test_shadow_local_for_struct_shorthand()807 fn test_shadow_local_for_struct_shorthand() {
808 check(
809 "j",
810 r#"
811 struct Foo { i: i32 }
812
813 fn baz(i$0: i32) -> Self {
814 let x = Foo { i };
815 {
816 let i = 0;
817 Foo { i }
818 }
819 }
820 "#,
821 r#"
822 struct Foo { i: i32 }
823
824 fn baz(j: i32) -> Self {
825 let x = Foo { i: j };
826 {
827 let i = 0;
828 Foo { i }
829 }
830 }
831 "#,
832 );
833 }
834
835 #[test]
test_rename_mod()836 fn test_rename_mod() {
837 check_expect(
838 "foo2",
839 r#"
840 //- /lib.rs
841 mod bar;
842
843 //- /bar.rs
844 mod foo$0;
845
846 //- /bar/foo.rs
847 // empty
848 "#,
849 expect![[r#"
850 SourceChange {
851 source_file_edits: {
852 FileId(
853 1,
854 ): TextEdit {
855 indels: [
856 Indel {
857 insert: "foo2",
858 delete: 4..7,
859 },
860 ],
861 },
862 },
863 file_system_edits: [
864 MoveFile {
865 src: FileId(
866 2,
867 ),
868 dst: AnchoredPathBuf {
869 anchor: FileId(
870 2,
871 ),
872 path: "foo2.rs",
873 },
874 },
875 ],
876 is_snippet: false,
877 }
878 "#]],
879 );
880 }
881
882 #[test]
test_rename_mod_in_use_tree()883 fn test_rename_mod_in_use_tree() {
884 check_expect(
885 "quux",
886 r#"
887 //- /main.rs
888 pub mod foo;
889 pub mod bar;
890 fn main() {}
891
892 //- /foo.rs
893 pub struct FooContent;
894
895 //- /bar.rs
896 use crate::foo$0::FooContent;
897 "#,
898 expect![[r#"
899 SourceChange {
900 source_file_edits: {
901 FileId(
902 0,
903 ): TextEdit {
904 indels: [
905 Indel {
906 insert: "quux",
907 delete: 8..11,
908 },
909 ],
910 },
911 FileId(
912 2,
913 ): TextEdit {
914 indels: [
915 Indel {
916 insert: "quux",
917 delete: 11..14,
918 },
919 ],
920 },
921 },
922 file_system_edits: [
923 MoveFile {
924 src: FileId(
925 1,
926 ),
927 dst: AnchoredPathBuf {
928 anchor: FileId(
929 1,
930 ),
931 path: "quux.rs",
932 },
933 },
934 ],
935 is_snippet: false,
936 }
937 "#]],
938 );
939 }
940
941 #[test]
test_rename_mod_in_dir()942 fn test_rename_mod_in_dir() {
943 check_expect(
944 "foo2",
945 r#"
946 //- /lib.rs
947 mod fo$0o;
948 //- /foo/mod.rs
949 // empty
950 "#,
951 expect![[r#"
952 SourceChange {
953 source_file_edits: {
954 FileId(
955 0,
956 ): TextEdit {
957 indels: [
958 Indel {
959 insert: "foo2",
960 delete: 4..7,
961 },
962 ],
963 },
964 },
965 file_system_edits: [
966 MoveFile {
967 src: FileId(
968 1,
969 ),
970 dst: AnchoredPathBuf {
971 anchor: FileId(
972 1,
973 ),
974 path: "../foo2/mod.rs",
975 },
976 },
977 ],
978 is_snippet: false,
979 }
980 "#]],
981 );
982 }
983
984 #[test]
test_rename_unusually_nested_mod()985 fn test_rename_unusually_nested_mod() {
986 check_expect(
987 "bar",
988 r#"
989 //- /lib.rs
990 mod outer { mod fo$0o; }
991
992 //- /outer/foo.rs
993 // empty
994 "#,
995 expect![[r#"
996 SourceChange {
997 source_file_edits: {
998 FileId(
999 0,
1000 ): TextEdit {
1001 indels: [
1002 Indel {
1003 insert: "bar",
1004 delete: 16..19,
1005 },
1006 ],
1007 },
1008 },
1009 file_system_edits: [
1010 MoveFile {
1011 src: FileId(
1012 1,
1013 ),
1014 dst: AnchoredPathBuf {
1015 anchor: FileId(
1016 1,
1017 ),
1018 path: "bar.rs",
1019 },
1020 },
1021 ],
1022 is_snippet: false,
1023 }
1024 "#]],
1025 );
1026 }
1027
1028 #[test]
test_module_rename_in_path()1029 fn test_module_rename_in_path() {
1030 check(
1031 "baz",
1032 r#"
1033 mod $0foo {
1034 pub use self::bar as qux;
1035 pub fn bar() {}
1036 }
1037
1038 fn main() { foo::bar(); }
1039 "#,
1040 r#"
1041 mod baz {
1042 pub use self::bar as qux;
1043 pub fn bar() {}
1044 }
1045
1046 fn main() { baz::bar(); }
1047 "#,
1048 );
1049 }
1050
1051 #[test]
test_rename_mod_filename_and_path()1052 fn test_rename_mod_filename_and_path() {
1053 check_expect(
1054 "foo2",
1055 r#"
1056 //- /lib.rs
1057 mod bar;
1058 fn f() {
1059 bar::foo::fun()
1060 }
1061
1062 //- /bar.rs
1063 pub mod foo$0;
1064
1065 //- /bar/foo.rs
1066 // pub fn fun() {}
1067 "#,
1068 expect![[r#"
1069 SourceChange {
1070 source_file_edits: {
1071 FileId(
1072 0,
1073 ): TextEdit {
1074 indels: [
1075 Indel {
1076 insert: "foo2",
1077 delete: 27..30,
1078 },
1079 ],
1080 },
1081 FileId(
1082 1,
1083 ): TextEdit {
1084 indels: [
1085 Indel {
1086 insert: "foo2",
1087 delete: 8..11,
1088 },
1089 ],
1090 },
1091 },
1092 file_system_edits: [
1093 MoveFile {
1094 src: FileId(
1095 2,
1096 ),
1097 dst: AnchoredPathBuf {
1098 anchor: FileId(
1099 2,
1100 ),
1101 path: "foo2.rs",
1102 },
1103 },
1104 ],
1105 is_snippet: false,
1106 }
1107 "#]],
1108 );
1109 }
1110
1111 #[test]
test_enum_variant_from_module_1()1112 fn test_enum_variant_from_module_1() {
1113 cov_mark::check!(rename_non_local);
1114 check(
1115 "Baz",
1116 r#"
1117 mod foo {
1118 pub enum Foo { Bar$0 }
1119 }
1120
1121 fn func(f: foo::Foo) {
1122 match f {
1123 foo::Foo::Bar => {}
1124 }
1125 }
1126 "#,
1127 r#"
1128 mod foo {
1129 pub enum Foo { Baz }
1130 }
1131
1132 fn func(f: foo::Foo) {
1133 match f {
1134 foo::Foo::Baz => {}
1135 }
1136 }
1137 "#,
1138 );
1139 }
1140
1141 #[test]
test_enum_variant_from_module_2()1142 fn test_enum_variant_from_module_2() {
1143 check(
1144 "baz",
1145 r#"
1146 mod foo {
1147 pub struct Foo { pub bar$0: uint }
1148 }
1149
1150 fn foo(f: foo::Foo) {
1151 let _ = f.bar;
1152 }
1153 "#,
1154 r#"
1155 mod foo {
1156 pub struct Foo { pub baz: uint }
1157 }
1158
1159 fn foo(f: foo::Foo) {
1160 let _ = f.baz;
1161 }
1162 "#,
1163 );
1164 }
1165
1166 #[test]
test_parameter_to_self()1167 fn test_parameter_to_self() {
1168 cov_mark::check!(rename_to_self);
1169 check(
1170 "self",
1171 r#"
1172 struct Foo { i: i32 }
1173
1174 impl Foo {
1175 fn f(foo$0: &mut Foo) -> i32 {
1176 foo.i
1177 }
1178 }
1179 "#,
1180 r#"
1181 struct Foo { i: i32 }
1182
1183 impl Foo {
1184 fn f(&mut self) -> i32 {
1185 self.i
1186 }
1187 }
1188 "#,
1189 );
1190 check(
1191 "self",
1192 r#"
1193 struct Foo { i: i32 }
1194
1195 impl Foo {
1196 fn f(foo$0: Foo) -> i32 {
1197 foo.i
1198 }
1199 }
1200 "#,
1201 r#"
1202 struct Foo { i: i32 }
1203
1204 impl Foo {
1205 fn f(self) -> i32 {
1206 self.i
1207 }
1208 }
1209 "#,
1210 );
1211 }
1212
1213 #[test]
test_parameter_to_self_error_no_impl()1214 fn test_parameter_to_self_error_no_impl() {
1215 check(
1216 "self",
1217 r#"
1218 struct Foo { i: i32 }
1219
1220 fn f(foo$0: &mut Foo) -> i32 {
1221 foo.i
1222 }
1223 "#,
1224 "error: Cannot rename parameter to self for free function",
1225 );
1226 check(
1227 "self",
1228 r#"
1229 struct Foo { i: i32 }
1230 struct Bar;
1231
1232 impl Bar {
1233 fn f(foo$0: &mut Foo) -> i32 {
1234 foo.i
1235 }
1236 }
1237 "#,
1238 "error: Parameter type differs from impl block type",
1239 );
1240 }
1241
1242 #[test]
test_parameter_to_self_error_not_first()1243 fn test_parameter_to_self_error_not_first() {
1244 check(
1245 "self",
1246 r#"
1247 struct Foo { i: i32 }
1248 impl Foo {
1249 fn f(x: (), foo$0: &mut Foo) -> i32 {
1250 foo.i
1251 }
1252 }
1253 "#,
1254 "error: Only the first parameter may be renamed to self",
1255 );
1256 }
1257
1258 #[test]
test_parameter_to_self_impl_ref()1259 fn test_parameter_to_self_impl_ref() {
1260 check(
1261 "self",
1262 r#"
1263 struct Foo { i: i32 }
1264 impl &Foo {
1265 fn f(foo$0: &Foo) -> i32 {
1266 foo.i
1267 }
1268 }
1269 "#,
1270 r#"
1271 struct Foo { i: i32 }
1272 impl &Foo {
1273 fn f(self) -> i32 {
1274 self.i
1275 }
1276 }
1277 "#,
1278 );
1279 }
1280
1281 #[test]
test_self_to_parameter()1282 fn test_self_to_parameter() {
1283 check(
1284 "foo",
1285 r#"
1286 struct Foo { i: i32 }
1287
1288 impl Foo {
1289 fn f(&mut $0self) -> i32 {
1290 self.i
1291 }
1292 }
1293 "#,
1294 r#"
1295 struct Foo { i: i32 }
1296
1297 impl Foo {
1298 fn f(foo: &mut Foo) -> i32 {
1299 foo.i
1300 }
1301 }
1302 "#,
1303 );
1304 }
1305
1306 #[test]
test_owned_self_to_parameter()1307 fn test_owned_self_to_parameter() {
1308 cov_mark::check!(rename_self_to_param);
1309 check(
1310 "foo",
1311 r#"
1312 struct Foo { i: i32 }
1313
1314 impl Foo {
1315 fn f($0self) -> i32 {
1316 self.i
1317 }
1318 }
1319 "#,
1320 r#"
1321 struct Foo { i: i32 }
1322
1323 impl Foo {
1324 fn f(foo: Foo) -> i32 {
1325 foo.i
1326 }
1327 }
1328 "#,
1329 );
1330 }
1331
1332 #[test]
test_self_in_path_to_parameter()1333 fn test_self_in_path_to_parameter() {
1334 check(
1335 "foo",
1336 r#"
1337 struct Foo { i: i32 }
1338
1339 impl Foo {
1340 fn f(&self) -> i32 {
1341 let self_var = 1;
1342 self$0.i
1343 }
1344 }
1345 "#,
1346 r#"
1347 struct Foo { i: i32 }
1348
1349 impl Foo {
1350 fn f(foo: &Foo) -> i32 {
1351 let self_var = 1;
1352 foo.i
1353 }
1354 }
1355 "#,
1356 );
1357 }
1358
1359 #[test]
test_rename_field_put_init_shorthand()1360 fn test_rename_field_put_init_shorthand() {
1361 cov_mark::check!(test_rename_field_put_init_shorthand);
1362 check(
1363 "bar",
1364 r#"
1365 struct Foo { i$0: i32 }
1366
1367 fn foo(bar: i32) -> Foo {
1368 Foo { i: bar }
1369 }
1370 "#,
1371 r#"
1372 struct Foo { bar: i32 }
1373
1374 fn foo(bar: i32) -> Foo {
1375 Foo { bar }
1376 }
1377 "#,
1378 );
1379 }
1380
1381 #[test]
test_rename_local_put_init_shorthand()1382 fn test_rename_local_put_init_shorthand() {
1383 cov_mark::check!(test_rename_local_put_init_shorthand);
1384 check(
1385 "i",
1386 r#"
1387 struct Foo { i: i32 }
1388
1389 fn foo(bar$0: i32) -> Foo {
1390 Foo { i: bar }
1391 }
1392 "#,
1393 r#"
1394 struct Foo { i: i32 }
1395
1396 fn foo(i: i32) -> Foo {
1397 Foo { i }
1398 }
1399 "#,
1400 );
1401 }
1402
1403 #[test]
test_struct_field_pat_into_shorthand()1404 fn test_struct_field_pat_into_shorthand() {
1405 cov_mark::check!(test_rename_field_put_init_shorthand_pat);
1406 check(
1407 "baz",
1408 r#"
1409 struct Foo { i$0: i32 }
1410
1411 fn foo(foo: Foo) {
1412 let Foo { i: ref baz @ qux } = foo;
1413 let _ = qux;
1414 }
1415 "#,
1416 r#"
1417 struct Foo { baz: i32 }
1418
1419 fn foo(foo: Foo) {
1420 let Foo { baz: ref baz @ qux } = foo;
1421 let _ = qux;
1422 }
1423 "#,
1424 );
1425 check(
1426 "baz",
1427 r#"
1428 struct Foo { i$0: i32 }
1429
1430 fn foo(foo: Foo) {
1431 let Foo { i: ref baz } = foo;
1432 let _ = qux;
1433 }
1434 "#,
1435 r#"
1436 struct Foo { baz: i32 }
1437
1438 fn foo(foo: Foo) {
1439 let Foo { ref baz } = foo;
1440 let _ = qux;
1441 }
1442 "#,
1443 );
1444 }
1445
1446 #[test]
test_struct_local_pat_into_shorthand()1447 fn test_struct_local_pat_into_shorthand() {
1448 cov_mark::check!(test_rename_local_put_init_shorthand_pat);
1449 check(
1450 "field",
1451 r#"
1452 struct Foo { field: i32 }
1453
1454 fn foo(foo: Foo) {
1455 let Foo { field: qux$0 } = foo;
1456 let _ = qux;
1457 }
1458 "#,
1459 r#"
1460 struct Foo { field: i32 }
1461
1462 fn foo(foo: Foo) {
1463 let Foo { field } = foo;
1464 let _ = field;
1465 }
1466 "#,
1467 );
1468 check(
1469 "field",
1470 r#"
1471 struct Foo { field: i32 }
1472
1473 fn foo(foo: Foo) {
1474 let Foo { field: x @ qux$0 } = foo;
1475 let _ = qux;
1476 }
1477 "#,
1478 r#"
1479 struct Foo { field: i32 }
1480
1481 fn foo(foo: Foo) {
1482 let Foo { field: x @ field } = foo;
1483 let _ = field;
1484 }
1485 "#,
1486 );
1487 }
1488
1489 #[test]
test_rename_binding_in_destructure_pat()1490 fn test_rename_binding_in_destructure_pat() {
1491 let expected_fixture = r#"
1492 struct Foo {
1493 i: i32,
1494 }
1495
1496 fn foo(foo: Foo) {
1497 let Foo { i: bar } = foo;
1498 let _ = bar;
1499 }
1500 "#;
1501 check(
1502 "bar",
1503 r#"
1504 struct Foo {
1505 i: i32,
1506 }
1507
1508 fn foo(foo: Foo) {
1509 let Foo { i: b } = foo;
1510 let _ = b$0;
1511 }
1512 "#,
1513 expected_fixture,
1514 );
1515 check(
1516 "bar",
1517 r#"
1518 struct Foo {
1519 i: i32,
1520 }
1521
1522 fn foo(foo: Foo) {
1523 let Foo { i } = foo;
1524 let _ = i$0;
1525 }
1526 "#,
1527 expected_fixture,
1528 );
1529 }
1530
1531 #[test]
test_rename_binding_in_destructure_param_pat()1532 fn test_rename_binding_in_destructure_param_pat() {
1533 check(
1534 "bar",
1535 r#"
1536 struct Foo {
1537 i: i32
1538 }
1539
1540 fn foo(Foo { i }: Foo) -> i32 {
1541 i$0
1542 }
1543 "#,
1544 r#"
1545 struct Foo {
1546 i: i32
1547 }
1548
1549 fn foo(Foo { i: bar }: Foo) -> i32 {
1550 bar
1551 }
1552 "#,
1553 )
1554 }
1555
1556 #[test]
test_struct_field_complex_ident_pat()1557 fn test_struct_field_complex_ident_pat() {
1558 cov_mark::check!(rename_record_pat_field_name_split);
1559 check(
1560 "baz",
1561 r#"
1562 struct Foo { i$0: i32 }
1563
1564 fn foo(foo: Foo) {
1565 let Foo { ref i } = foo;
1566 }
1567 "#,
1568 r#"
1569 struct Foo { baz: i32 }
1570
1571 fn foo(foo: Foo) {
1572 let Foo { baz: ref i } = foo;
1573 }
1574 "#,
1575 );
1576 }
1577
1578 #[test]
test_rename_lifetimes()1579 fn test_rename_lifetimes() {
1580 cov_mark::check!(rename_lifetime);
1581 check(
1582 "'yeeee",
1583 r#"
1584 trait Foo<'a> {
1585 fn foo() -> &'a ();
1586 }
1587 impl<'a> Foo<'a> for &'a () {
1588 fn foo() -> &'a$0 () {
1589 unimplemented!()
1590 }
1591 }
1592 "#,
1593 r#"
1594 trait Foo<'a> {
1595 fn foo() -> &'a ();
1596 }
1597 impl<'yeeee> Foo<'yeeee> for &'yeeee () {
1598 fn foo() -> &'yeeee () {
1599 unimplemented!()
1600 }
1601 }
1602 "#,
1603 )
1604 }
1605
1606 #[test]
test_rename_bind_pat()1607 fn test_rename_bind_pat() {
1608 check(
1609 "new_name",
1610 r#"
1611 fn main() {
1612 enum CustomOption<T> {
1613 None,
1614 Some(T),
1615 }
1616
1617 let test_variable = CustomOption::Some(22);
1618
1619 match test_variable {
1620 CustomOption::Some(foo$0) if foo == 11 => {}
1621 _ => (),
1622 }
1623 }"#,
1624 r#"
1625 fn main() {
1626 enum CustomOption<T> {
1627 None,
1628 Some(T),
1629 }
1630
1631 let test_variable = CustomOption::Some(22);
1632
1633 match test_variable {
1634 CustomOption::Some(new_name) if new_name == 11 => {}
1635 _ => (),
1636 }
1637 }"#,
1638 );
1639 }
1640
1641 #[test]
test_rename_label()1642 fn test_rename_label() {
1643 check(
1644 "'foo",
1645 r#"
1646 fn foo<'a>() -> &'a () {
1647 'a: {
1648 'b: loop {
1649 break 'a$0;
1650 }
1651 }
1652 }
1653 "#,
1654 r#"
1655 fn foo<'a>() -> &'a () {
1656 'foo: {
1657 'b: loop {
1658 break 'foo;
1659 }
1660 }
1661 }
1662 "#,
1663 )
1664 }
1665
1666 #[test]
test_self_to_self()1667 fn test_self_to_self() {
1668 cov_mark::check!(rename_self_to_self);
1669 check(
1670 "self",
1671 r#"
1672 struct Foo;
1673 impl Foo {
1674 fn foo(self$0) {}
1675 }
1676 "#,
1677 r#"
1678 struct Foo;
1679 impl Foo {
1680 fn foo(self) {}
1681 }
1682 "#,
1683 )
1684 }
1685
1686 #[test]
test_rename_field_in_pat_in_macro_doesnt_shorthand()1687 fn test_rename_field_in_pat_in_macro_doesnt_shorthand() {
1688 // ideally we would be able to make this emit a short hand, but I doubt this is easily possible
1689 check(
1690 "baz",
1691 r#"
1692 macro_rules! foo {
1693 ($pattern:pat) => {
1694 let $pattern = loop {};
1695 };
1696 }
1697 struct Foo {
1698 bar$0: u32,
1699 }
1700 fn foo() {
1701 foo!(Foo { bar: baz });
1702 }
1703 "#,
1704 r#"
1705 macro_rules! foo {
1706 ($pattern:pat) => {
1707 let $pattern = loop {};
1708 };
1709 }
1710 struct Foo {
1711 baz: u32,
1712 }
1713 fn foo() {
1714 foo!(Foo { baz: baz });
1715 }
1716 "#,
1717 )
1718 }
1719
1720 #[test]
test_rename_tuple_field()1721 fn test_rename_tuple_field() {
1722 check(
1723 "foo",
1724 r#"
1725 struct Foo(i32);
1726
1727 fn baz() {
1728 let mut x = Foo(4);
1729 x.0$0 = 5;
1730 }
1731 "#,
1732 "error: No identifier available to rename",
1733 );
1734 }
1735
1736 #[test]
test_rename_builtin()1737 fn test_rename_builtin() {
1738 check(
1739 "foo",
1740 r#"
1741 fn foo() {
1742 let x: i32$0 = 0;
1743 }
1744 "#,
1745 "error: Cannot rename builtin type",
1746 );
1747 }
1748
1749 #[test]
test_rename_self()1750 fn test_rename_self() {
1751 check(
1752 "foo",
1753 r#"
1754 struct Foo {}
1755
1756 impl Foo {
1757 fn foo(self) -> Self$0 {
1758 self
1759 }
1760 }
1761 "#,
1762 "error: Cannot rename `Self`",
1763 );
1764 }
1765
1766 #[test]
test_rename_ignores_self_ty()1767 fn test_rename_ignores_self_ty() {
1768 check(
1769 "Fo0",
1770 r#"
1771 struct $0Foo;
1772
1773 impl Foo where Self: {}
1774 "#,
1775 r#"
1776 struct Fo0;
1777
1778 impl Fo0 where Self: {}
1779 "#,
1780 );
1781 }
1782
1783 #[test]
test_rename_fails_on_aliases()1784 fn test_rename_fails_on_aliases() {
1785 check(
1786 "Baz",
1787 r#"
1788 struct Foo;
1789 use Foo as Bar$0;
1790 "#,
1791 "error: Renaming aliases is currently unsupported",
1792 );
1793 check(
1794 "Baz",
1795 r#"
1796 struct Foo;
1797 use Foo as Bar;
1798 use Bar$0;
1799 "#,
1800 "error: Renaming aliases is currently unsupported",
1801 );
1802 }
1803
1804 #[test]
test_rename_trait_method()1805 fn test_rename_trait_method() {
1806 let res = r"
1807 trait Foo {
1808 fn foo(&self) {
1809 self.foo();
1810 }
1811 }
1812
1813 impl Foo for () {
1814 fn foo(&self) {
1815 self.foo();
1816 }
1817 }";
1818 check(
1819 "foo",
1820 r#"
1821 trait Foo {
1822 fn bar$0(&self) {
1823 self.bar();
1824 }
1825 }
1826
1827 impl Foo for () {
1828 fn bar(&self) {
1829 self.bar();
1830 }
1831 }"#,
1832 res,
1833 );
1834 check(
1835 "foo",
1836 r#"
1837 trait Foo {
1838 fn bar(&self) {
1839 self.bar$0();
1840 }
1841 }
1842
1843 impl Foo for () {
1844 fn bar(&self) {
1845 self.bar();
1846 }
1847 }"#,
1848 res,
1849 );
1850 check(
1851 "foo",
1852 r#"
1853 trait Foo {
1854 fn bar(&self) {
1855 self.bar();
1856 }
1857 }
1858
1859 impl Foo for () {
1860 fn bar$0(&self) {
1861 self.bar();
1862 }
1863 }"#,
1864 res,
1865 );
1866 check(
1867 "foo",
1868 r#"
1869 trait Foo {
1870 fn bar(&self) {
1871 self.bar();
1872 }
1873 }
1874
1875 impl Foo for () {
1876 fn bar(&self) {
1877 self.bar$0();
1878 }
1879 }"#,
1880 res,
1881 );
1882 }
1883
1884 #[test]
test_rename_trait_method_prefix_of_second()1885 fn test_rename_trait_method_prefix_of_second() {
1886 check(
1887 "qux",
1888 r#"
1889 trait Foo {
1890 fn foo$0() {}
1891 fn foobar() {}
1892 }
1893 "#,
1894 r#"
1895 trait Foo {
1896 fn qux() {}
1897 fn foobar() {}
1898 }
1899 "#,
1900 );
1901 }
1902
1903 #[test]
test_rename_trait_const()1904 fn test_rename_trait_const() {
1905 let res = r"
1906 trait Foo {
1907 const FOO: ();
1908 }
1909
1910 impl Foo for () {
1911 const FOO: ();
1912 }
1913 fn f() { <()>::FOO; }";
1914 check(
1915 "FOO",
1916 r#"
1917 trait Foo {
1918 const BAR$0: ();
1919 }
1920
1921 impl Foo for () {
1922 const BAR: ();
1923 }
1924 fn f() { <()>::BAR; }"#,
1925 res,
1926 );
1927 check(
1928 "FOO",
1929 r#"
1930 trait Foo {
1931 const BAR: ();
1932 }
1933
1934 impl Foo for () {
1935 const BAR$0: ();
1936 }
1937 fn f() { <()>::BAR; }"#,
1938 res,
1939 );
1940 check(
1941 "FOO",
1942 r#"
1943 trait Foo {
1944 const BAR: ();
1945 }
1946
1947 impl Foo for () {
1948 const BAR: ();
1949 }
1950 fn f() { <()>::BAR$0; }"#,
1951 res,
1952 );
1953 }
1954
1955 #[test]
defs_from_macros_arent_renamed()1956 fn defs_from_macros_arent_renamed() {
1957 check(
1958 "lol",
1959 r#"
1960 macro_rules! m { () => { fn f() {} } }
1961 m!();
1962 fn main() { f$0() }
1963 "#,
1964 "error: No identifier available to rename",
1965 )
1966 }
1967
1968 #[test]
attributed_item()1969 fn attributed_item() {
1970 check(
1971 "function",
1972 r#"
1973 //- proc_macros: identity
1974
1975 #[proc_macros::identity]
1976 fn func$0() {
1977 func();
1978 }
1979 "#,
1980 r#"
1981
1982 #[proc_macros::identity]
1983 fn function() {
1984 function();
1985 }
1986 "#,
1987 )
1988 }
1989
1990 #[test]
in_macro_multi_mapping()1991 fn in_macro_multi_mapping() {
1992 check(
1993 "a",
1994 r#"
1995 fn foo() {
1996 macro_rules! match_ast2 {
1997 ($node:ident {
1998 $( $res:expr, )*
1999 }) => {{
2000 $( if $node { $res } else )*
2001 { loop {} }
2002 }};
2003 }
2004 let $0d = 3;
2005 match_ast2! {
2006 d {
2007 d,
2008 d,
2009 }
2010 };
2011 }
2012 "#,
2013 r#"
2014 fn foo() {
2015 macro_rules! match_ast2 {
2016 ($node:ident {
2017 $( $res:expr, )*
2018 }) => {{
2019 $( if $node { $res } else )*
2020 { loop {} }
2021 }};
2022 }
2023 let a = 3;
2024 match_ast2! {
2025 a {
2026 a,
2027 a,
2028 }
2029 };
2030 }
2031 "#,
2032 )
2033 }
2034 }
2035