Skip to content

Observe unsafeness when generating manual impls of former derives #19320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syntax::{
SyntaxKind::WHITESPACE,
T,
ast::{self, AstNode, HasName, make},
ted,
ted::{self, Position},
};

use crate::{
Expand Down Expand Up @@ -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);
Expand All @@ -141,13 +141,25 @@ 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,
vec![make::tokens::blank_line().into(), impl_def.syntax().clone().into()],
);
}
(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()],
Expand All @@ -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())
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -1402,6 +1429,23 @@ impl core::fmt::Debug for Foo {
f.debug_struct("Foo").finish()
}
}
"#,
)
}

#[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}
"#,
)
}
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
});
Expand Down