From d30f2d43dbfeb63f781378ef4243695debb082a0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Jun 2021 13:15:07 +0200 Subject: Prevent renaming of aliases on usages --- crates/ide/src/references/rename.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 01fe3a1a1..2a4a1c3c8 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -170,7 +170,17 @@ fn find_definition( NameClass::classify(sema, &name).map(|class| class.referenced_or_defined(sema.db)) } ast::NameLike::NameRef(name_ref) => { - NameRefClass::classify(sema, &name_ref).map(|class| class.referenced(sema.db)) + if let Some(def) = + NameRefClass::classify(sema, &name_ref).map(|class| class.referenced(sema.db)) + { + // if the name differs from the definitions name it has to be an alias + if def.name(sema.db).map_or(false, |it| it.to_string() != name_ref.text()) { + bail!("Renaming aliases is currently unsupported"); + } + Some(def) + } else { + None + } } ast::NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime) .map(|class| NameRefClass::referenced(class, sema.db)) @@ -1907,4 +1917,25 @@ impl Fo0 where Self: {} "#, ); } + + #[test] + fn test_rename_fails_on_aliases() { + check( + "Baz", + r#" +struct Foo; +use Foo as Bar$0; +"#, + "error: Renaming aliases is currently unsupported", + ); + check( + "Baz", + r#" +struct Foo; +use Foo as Bar; +use Bar$0; +"#, + "error: Renaming aliases is currently unsupported", + ); + } } -- cgit v1.2.3