Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #19320 from alibektas/19254
Observe unsafeness when generating manual impls of former derives
Chayim Refael Friedman 2025-03-18
parent 0c1b483 · parent 1afbcc0 · commit 31e412c
-rw-r--r--crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs48
-rw-r--r--crates/ide-assists/src/utils.rs1
-rw-r--r--crates/syntax/src/ast/make.rs2
3 files changed, 48 insertions, 3 deletions
diff --git a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
index 7696a14a4f..e0693a7d5b 100644
--- a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
+++ b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
@@ -5,7 +5,7 @@ use syntax::{
SyntaxKind::WHITESPACE,
T,
ast::{self, AstNode, HasName, make},
- ted,
+ ted::{self, Position},
};
use crate::{
@@ -131,7 +131,7 @@ fn add_assist(
target,
|builder| {
let insert_after = ted::Position::after(builder.make_mut(adt.clone()).syntax());
-
+ let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
let impl_def_with_items =
impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path);
update_attribute(builder, old_derives, old_tree, old_trait_path, attr);
@@ -141,6 +141,12 @@ fn add_assist(
match (ctx.config.snippet_cap, impl_def_with_items) {
(None, None) => {
let impl_def = generate_trait_impl(adt, trait_path);
+ if impl_is_unsafe {
+ ted::insert(
+ Position::first_child_of(impl_def.syntax()),
+ make::token(T![unsafe]),
+ );
+ }
ted::insert_all(
insert_after,
@@ -148,6 +154,12 @@ fn add_assist(
);
}
(None, Some((impl_def, _))) => {
+ if impl_is_unsafe {
+ ted::insert(
+ Position::first_child_of(impl_def.syntax()),
+ make::token(T![unsafe]),
+ );
+ }
ted::insert_all(
insert_after,
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
@@ -156,6 +168,13 @@ fn add_assist(
(Some(cap), None) => {
let impl_def = generate_trait_impl(adt, trait_path);
+ if impl_is_unsafe {
+ ted::insert(
+ Position::first_child_of(impl_def.syntax()),
+ make::token(T![unsafe]),
+ );
+ }
+
if let Some(l_curly) =
impl_def.assoc_item_list().and_then(|it| it.l_curly_token())
{
@@ -169,6 +188,14 @@ fn add_assist(
}
(Some(cap), Some((impl_def, first_assoc_item))) => {
let mut added_snippet = false;
+
+ if impl_is_unsafe {
+ ted::insert(
+ Position::first_child_of(impl_def.syntax()),
+ make::token(T![unsafe]),
+ );
+ }
+
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
{
@@ -1405,4 +1432,21 @@ impl core::fmt::Debug for Foo {
"#,
)
}
+
+ #[test]
+ fn unsafeness_of_a_trait_observed() {
+ check_assist(
+ replace_derive_with_manual_impl,
+ r#"
+//- minicore: send, derive
+#[derive(Sen$0d)]
+pub struct Foo;
+"#,
+ r#"
+pub struct Foo;
+
+unsafe impl Send for Foo {$0}
+"#,
+ )
+ }
}
diff --git a/crates/ide-assists/src/utils.rs b/crates/ide-assists/src/utils.rs
index 0806d5feb1..8b81c920ec 100644
--- a/crates/ide-assists/src/utils.rs
+++ b/crates/ide-assists/src/utils.rs
@@ -212,6 +212,7 @@ pub fn add_trait_assoc_items_to_impl(
});
let assoc_item_list = impl_.get_or_create_assoc_item_list();
+
let mut first_item = None;
for item in items {
first_item.get_or_insert_with(|| item.clone());
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 7147311f63..d5a5119ff6 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -1276,7 +1276,7 @@ pub mod tokens {
pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| {
SourceFile::parse(
- "use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nimpl A for B where: {}",
+ "use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nunsafe impl A for B where: {}",
Edition::CURRENT,
)
});