Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Aug 24, 2023
1 parent 9ad7613 commit 16835b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 11 additions & 3 deletions crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! Our current behavior is ¯\_(ツ)_/¯.
use std::fmt;

use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange};
use base_db::{AnchoredPathBuf, FileId, FileRange};
use either::Either;
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
use stdx::never;
Expand Down Expand Up @@ -71,21 +71,29 @@ impl Definition {
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.
if let Some(krate) = self.krate(sema.db) {
if !matches!(krate.origin(sema.db), CrateOrigin::Local { .. }) {
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) => {
if mac.is_builtin_derive(sema.db) {
bail!("Cannot rename builtin macro")
bail!("Cannot rename builtin derive")
}

rename_reference(sema, Definition::Macro(mac), new_name)
Expand Down
23 changes: 17 additions & 6 deletions crates/ide/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,6 @@ fn main() {
",
)
}
<<<<<<< HEAD

#[test]
fn extern_crate() {
Expand Down Expand Up @@ -2635,21 +2634,33 @@ use qux as frob;
// ",
// );
}
||||||| parent of 948d9f274 (Disallow renaming of non-local structs)
=======

#[test]
fn disallow_renaming_for_non_local_definition() {
check(
"Baz",
r#"
//- /lib.rs crate:lib new_source_root:library
pub struct S$0;
pub struct S;
//- /main.rs crate:main deps:lib new_source_root:local
use lib::S;
use lib::S$0;
"#,
"error: Cannot rename a non-local definition.",
);
}
>>>>>>> 948d9f274 (Disallow renaming of non-local structs)

#[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.",
)
}
}

0 comments on commit 16835b8

Please sign in to comment.