Skip to content

Commit 948d9f2

Browse files
committed
Disallow renaming of non-local structs
1 parent 2d83bc5 commit 948d9f2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

crates/ide-db/src/rename.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! Our current behavior is ¯\_(ツ)_/¯.
2323
use std::fmt;
2424

25-
use base_db::{AnchoredPathBuf, FileId, FileRange};
25+
use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange};
2626
use either::Either;
2727
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
2828
use stdx::never;
@@ -77,6 +77,15 @@ impl Definition {
7777
bail!("Cannot rename builtin type")
7878
}
7979
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
80+
Definition::Adt(hir::Adt::Struct(strukt)) => {
81+
if !matches!(
82+
strukt.module(sema.db).krate().origin(sema.db),
83+
CrateOrigin::Local { .. }
84+
) {
85+
bail!("Cannot rename a non-local struct.")
86+
}
87+
rename_reference(sema, Definition::Adt(hir::Adt::Struct(strukt)), new_name)
88+
}
8089
def => rename_reference(sema, def, new_name),
8190
}
8291
}

crates/ide/src/rename.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2487,4 +2487,18 @@ fn main() {
24872487
",
24882488
)
24892489
}
2490+
2491+
#[test]
2492+
fn disallow_renaming_for_non_local_struct() {
2493+
check(
2494+
"Baz",
2495+
r#"
2496+
//- /lib.rs crate:lib new_source_root:library
2497+
pub struct S$0;
2498+
//- /main.rs crate:main deps:lib new_source_root:local
2499+
use lib::S;
2500+
"#,
2501+
"error: Cannot rename a non-local struct.",
2502+
);
2503+
}
24902504
}

0 commit comments

Comments
 (0)