Skip to content

Commit 1afbcc0

Browse files
committed
Test unsafeness is respected when manual impling derives
1 parent 824df43 commit 1afbcc0

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{
@@ -223,10 +250,6 @@ fn impl_def_from_trait(
223250
let first_assoc_item =
224251
add_trait_assoc_items_to_impl(sema, &trait_items, trait_, &impl_def, &target_scope);
225252

226-
if trait_.is_unsafe(sema.db) {
227-
ted::insert(Position::first_child_of(impl_def.syntax()), make::token(T![unsafe]));
228-
}
229-
230253
// Generate a default `impl` function body for the derived trait.
231254
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
232255
let _ = gen_trait_fn_body(func, trait_path, adt, None);
@@ -1406,6 +1429,23 @@ impl core::fmt::Debug for Foo {
14061429
f.debug_struct("Foo").finish()
14071430
}
14081431
}
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}
14091449
"#,
14101450
)
14111451
}

0 commit comments

Comments
 (0)