Skip to content

Commit 7ae70a0

Browse files
committed
Disallow renaming of non-local structs
1 parent 326f37e commit 7ae70a0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

crates/ide-db/src/rename.rs

Lines changed: 10 additions & 1 deletion
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,7 @@ fn main() {
25292529
",
25302530
)
25312531
}
2532+
<<<<<<< HEAD
25322533

25332534
#[test]
25342535
fn extern_crate() {
@@ -2634,4 +2635,21 @@ use qux as frob;
26342635
// ",
26352636
// );
26362637
}
2638+
||||||| parent of 948d9f274 (Disallow renaming of non-local structs)
2639+
=======
2640+
2641+
#[test]
2642+
fn disallow_renaming_for_non_local_struct() {
2643+
check(
2644+
"Baz",
2645+
r#"
2646+
//- /lib.rs crate:lib new_source_root:library
2647+
pub struct S$0;
2648+
//- /main.rs crate:main deps:lib new_source_root:local
2649+
use lib::S;
2650+
"#,
2651+
"error: Cannot rename a non-local struct.",
2652+
);
2653+
}
2654+
>>>>>>> 948d9f274 (Disallow renaming of non-local structs)
26372655
}

0 commit comments

Comments
 (0)