From 7ae70a06ceb9b6a2619bb91f8d839417c3ee3f67 Mon Sep 17 00:00:00 2001 From: Ali Bektas Date: Sat, 8 Jul 2023 02:39:03 +0200 Subject: [PATCH] Disallow renaming of non-local structs --- crates/ide-db/src/rename.rs | 11 ++++++++++- crates/ide/src/rename.rs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/ide-db/src/rename.rs b/crates/ide-db/src/rename.rs index aa0bb7cce69..27add0ad371 100644 --- a/crates/ide-db/src/rename.rs +++ b/crates/ide-db/src/rename.rs @@ -22,7 +22,7 @@ //! Our current behavior is ¯\_(ツ)_/¯. use std::fmt; -use base_db::{AnchoredPathBuf, FileId, FileRange}; +use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange}; use either::Either; use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics}; use stdx::never; @@ -77,6 +77,15 @@ impl Definition { bail!("Cannot rename builtin type") } Definition::SelfType(_) => bail!("Cannot rename `Self`"), + Definition::Adt(hir::Adt::Struct(strukt)) => { + if !matches!( + strukt.module(sema.db).krate().origin(sema.db), + CrateOrigin::Local { .. } + ) { + bail!("Cannot rename a non-local struct.") + } + rename_reference(sema, Definition::Adt(hir::Adt::Struct(strukt)), new_name) + } def => rename_reference(sema, def, new_name), } } diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index dae8e71e8a0..3017ca75366 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -2529,6 +2529,7 @@ fn main() { ", ) } +<<<<<<< HEAD #[test] fn extern_crate() { @@ -2634,4 +2635,21 @@ use qux as frob; // ", // ); } +||||||| parent of 948d9f274 (Disallow renaming of non-local structs) +======= + + #[test] + fn disallow_renaming_for_non_local_struct() { + check( + "Baz", + r#" +//- /lib.rs crate:lib new_source_root:library +pub struct S$0; +//- /main.rs crate:main deps:lib new_source_root:local +use lib::S; +"#, + "error: Cannot rename a non-local struct.", + ); + } +>>>>>>> 948d9f274 (Disallow renaming of non-local structs) }