Auto merge of #15232 - alibektas:14850, r=Veykril

ide : Disallow renaming of non-local items

fixes #14850 . This makes me wonder , why stop at structs and not do the same for other ADTs? Would be happy to add them too if nothing speaks against it.
This commit is contained in:
bors 2023-09-11 08:21:51 +00:00
commit 0e251ff768
2 changed files with 46 additions and 0 deletions

View File

@ -71,12 +71,29 @@ pub fn rename(
sema: &Semantics<'_, RootDatabase>,
new_name: &str,
) -> Result<SourceChange> {
// self.krate() returns None if
// self is a built-in attr, built-in type or tool module.
// it is not allowed for these defs to be renamed.
// cases where self.krate() is None is handled below.
if let Some(krate) = self.krate(sema.db) {
if !krate.origin(sema.db).is_local() {
bail!("Cannot rename a non-local definition.")
}
}
match *self {
Definition::Module(module) => rename_mod(sema, module, new_name),
Definition::ToolModule(_) => {
bail!("Cannot rename a tool module")
}
Definition::BuiltinType(_) => {
bail!("Cannot rename builtin type")
}
Definition::BuiltinAttr(_) => {
bail!("Cannot rename a builtin attr.")
}
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::Macro(mac) => rename_reference(sema, Definition::Macro(mac), new_name),
def => rename_reference(sema, def, new_name),
}
}

View File

@ -2634,4 +2634,33 @@ fn extern_crate_self_rename() {
// ",
// );
}
#[test]
fn disallow_renaming_for_non_local_definition() {
check(
"Baz",
r#"
//- /lib.rs crate:lib new_source_root:library
pub struct S;
//- /main.rs crate:main deps:lib new_source_root:local
use lib::S$0;
"#,
"error: Cannot rename a non-local definition.",
);
}
#[test]
fn disallow_renaming_for_builtin_macros() {
check(
"Baz",
r#"
//- minicore: derive, hash
//- /main.rs crate:main
use core::hash::Hash;
#[derive(H$0ash)]
struct A;
"#,
"error: Cannot rename a non-local definition.",
)
}
}