Skip to content

Commit 31e412c

Browse files
Merge pull request #19320 from alibektas/19254
Observe unsafeness when generating manual impls of former derives
2 parents 0c1b483 + 1afbcc0 commit 31e412c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::{
55
SyntaxKind::WHITESPACE,
66
T,
77
ast::{self, AstNode, HasName, make},
8-
ted,
8+
ted::{self, Position},
99
};
1010

1111
use crate::{
@@ -131,7 +131,7 @@ fn add_assist(
131131
target,
132132
|builder| {
133133
let insert_after = ted::Position::after(builder.make_mut(adt.clone()).syntax());
134-
134+
let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
135135
let impl_def_with_items =
136136
impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path);
137137
update_attribute(builder, old_derives, old_tree, old_trait_path, attr);
@@ -141,13 +141,25 @@ fn add_assist(
141141
match (ctx.config.snippet_cap, impl_def_with_items) {
142142
(None, None) => {
143143
let impl_def = generate_trait_impl(adt, trait_path);
144+
if impl_is_unsafe {
145+
ted::insert(
146+
Position::first_child_of(impl_def.syntax()),
147+
make::token(T![unsafe]),
148+
);
149+
}
144150

145151
ted::insert_all(
146152
insert_after,
147153
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
148154
);
149155
}
150156
(None, Some((impl_def, _))) => {
157+
if impl_is_unsafe {
158+
ted::insert(
159+
Position::first_child_of(impl_def.syntax()),
160+
make::token(T![unsafe]),
161+
);
162+
}
151163
ted::insert_all(
152164
insert_after,
153165
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
@@ -156,6 +168,13 @@ fn add_assist(
156168
(Some(cap), None) => {
157169
let impl_def = generate_trait_impl(adt, trait_path);
158170

171+
if impl_is_unsafe {
172+
ted::insert(
173+
Position::first_child_of(impl_def.syntax()),
174+
make::token(T![unsafe]),
175+
);
176+
}
177+
159178
if let Some(l_curly) =
160179
impl_def.assoc_item_list().and_then(|it| it.l_curly_token())
161180
{
@@ -169,6 +188,14 @@ fn add_assist(
169188
}
170189
(Some(cap), Some((impl_def, first_assoc_item))) => {
171190
let mut added_snippet = false;
191+
192+
if impl_is_unsafe {
193+
ted::insert(
194+
Position::first_child_of(impl_def.syntax()),
195+
make::token(T![unsafe]),
196+
);
197+
}
198+
172199
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
173200
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
174201
{
@@ -1402,6 +1429,23 @@ impl core::fmt::Debug for Foo {
14021429
f.debug_struct("Foo").finish()
14031430
}
14041431
}
1432+
"#,
1433+
)
1434+
}
1435+
1436+
#[test]
1437+
fn unsafeness_of_a_trait_observed() {
1438+
check_assist(
1439+
replace_derive_with_manual_impl,
1440+
r#"
1441+
//- minicore: send, derive
1442+
#[derive(Sen$0d)]
1443+
pub struct Foo;
1444+
"#,
1445+
r#"
1446+
pub struct Foo;
1447+
1448+
unsafe impl Send for Foo {$0}
14051449
"#,
14061450
)
14071451
}

crates/ide-assists/src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub fn add_trait_assoc_items_to_impl(
212212
});
213213

214214
let assoc_item_list = impl_.get_or_create_assoc_item_list();
215+
215216
let mut first_item = None;
216217
for item in items {
217218
first_item.get_or_insert_with(|| item.clone());

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ pub mod tokens {
12761276

12771277
pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| {
12781278
SourceFile::parse(
1279-
"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: {}",
1279+
"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: {}",
12801280
Edition::CURRENT,
12811281
)
12821282
});

0 commit comments

Comments
 (0)