@@ -5,7 +5,7 @@ use syntax::{
5
5
SyntaxKind :: WHITESPACE ,
6
6
T ,
7
7
ast:: { self , AstNode , HasName , make} ,
8
- ted,
8
+ ted:: { self , Position } ,
9
9
} ;
10
10
11
11
use crate :: {
@@ -131,7 +131,7 @@ fn add_assist(
131
131
target,
132
132
|builder| {
133
133
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 ) ;
135
135
let impl_def_with_items =
136
136
impl_def_from_trait ( & ctx. sema , adt, & annotated_name, trait_, replace_trait_path) ;
137
137
update_attribute ( builder, old_derives, old_tree, old_trait_path, attr) ;
@@ -141,13 +141,25 @@ fn add_assist(
141
141
match ( ctx. config . snippet_cap , impl_def_with_items) {
142
142
( None , None ) => {
143
143
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
+ }
144
150
145
151
ted:: insert_all (
146
152
insert_after,
147
153
vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_def. syntax( ) . clone( ) . into( ) ] ,
148
154
) ;
149
155
}
150
156
( 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
+ }
151
163
ted:: insert_all (
152
164
insert_after,
153
165
vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_def. syntax( ) . clone( ) . into( ) ] ,
@@ -156,6 +168,13 @@ fn add_assist(
156
168
( Some ( cap) , None ) => {
157
169
let impl_def = generate_trait_impl ( adt, trait_path) ;
158
170
171
+ if impl_is_unsafe {
172
+ ted:: insert (
173
+ Position :: first_child_of ( impl_def. syntax ( ) ) ,
174
+ make:: token ( T ! [ unsafe ] ) ,
175
+ ) ;
176
+ }
177
+
159
178
if let Some ( l_curly) =
160
179
impl_def. assoc_item_list ( ) . and_then ( |it| it. l_curly_token ( ) )
161
180
{
@@ -169,6 +188,14 @@ fn add_assist(
169
188
}
170
189
( Some ( cap) , Some ( ( impl_def, first_assoc_item) ) ) => {
171
190
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
+
172
199
if let ast:: AssocItem :: Fn ( ref func) = first_assoc_item {
173
200
if let Some ( m) = func. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast)
174
201
{
@@ -1402,6 +1429,23 @@ impl core::fmt::Debug for Foo {
1402
1429
f.debug_struct("Foo").finish()
1403
1430
}
1404
1431
}
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}
1405
1449
"# ,
1406
1450
)
1407
1451
}
0 commit comments