Skip to content

Commit 0e251ff

Browse files
committed
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.
2 parents 326f37e + 0118741 commit 0e251ff

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

crates/ide-db/src/rename.rs

+17
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,29 @@ impl Definition {
7171
sema: &Semantics<'_, RootDatabase>,
7272
new_name: &str,
7373
) -> Result<SourceChange> {
74+
// self.krate() returns None if
75+
// self is a built-in attr, built-in type or tool module.
76+
// it is not allowed for these defs to be renamed.
77+
// cases where self.krate() is None is handled below.
78+
if let Some(krate) = self.krate(sema.db) {
79+
if !krate.origin(sema.db).is_local() {
80+
bail!("Cannot rename a non-local definition.")
81+
}
82+
}
83+
7484
match *self {
7585
Definition::Module(module) => rename_mod(sema, module, new_name),
86+
Definition::ToolModule(_) => {
87+
bail!("Cannot rename a tool module")
88+
}
7689
Definition::BuiltinType(_) => {
7790
bail!("Cannot rename builtin type")
7891
}
92+
Definition::BuiltinAttr(_) => {
93+
bail!("Cannot rename a builtin attr.")
94+
}
7995
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
96+
Definition::Macro(mac) => rename_reference(sema, Definition::Macro(mac), new_name),
8097
def => rename_reference(sema, def, new_name),
8198
}
8299
}

crates/ide/src/rename.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2634,4 +2634,33 @@ use qux as frob;
26342634
// ",
26352635
// );
26362636
}
2637+
2638+
#[test]
2639+
fn disallow_renaming_for_non_local_definition() {
2640+
check(
2641+
"Baz",
2642+
r#"
2643+
//- /lib.rs crate:lib new_source_root:library
2644+
pub struct S;
2645+
//- /main.rs crate:main deps:lib new_source_root:local
2646+
use lib::S$0;
2647+
"#,
2648+
"error: Cannot rename a non-local definition.",
2649+
);
2650+
}
2651+
2652+
#[test]
2653+
fn disallow_renaming_for_builtin_macros() {
2654+
check(
2655+
"Baz",
2656+
r#"
2657+
//- minicore: derive, hash
2658+
//- /main.rs crate:main
2659+
use core::hash::Hash;
2660+
#[derive(H$0ash)]
2661+
struct A;
2662+
"#,
2663+
"error: Cannot rename a non-local definition.",
2664+
)
2665+
}
26372666
}

0 commit comments

Comments
 (0)