@@ -52,17 +52,25 @@ pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>)
52
52
== FamousDefs ( sema, scope. krate ( ) ) . core_convert_Into ( ) ?
53
53
{
54
54
let type_call = sema. type_of_expr ( & method_call. clone ( ) . into ( ) ) ?;
55
- let type_call_disp =
56
- type_call. adjusted ( ) . display_source_code ( db, scope. module ( ) . into ( ) , true ) . ok ( ) ?;
55
+ let adjusted_tc = type_call. adjusted ( ) ;
57
56
57
+ if adjusted_tc. contains_unknown ( ) {
58
+ return None ;
59
+ }
60
+
61
+ let sc = adjusted_tc. display_source_code ( db, scope. module ( ) . into ( ) , true ) . ok ( ) ?;
58
62
acc. add (
59
63
AssistId ( "into_to_qualified_from" , AssistKind :: Generate ) ,
60
64
"Convert `into` to fully qualified `from`" ,
61
65
nameref. syntax ( ) . text_range ( ) ,
62
66
|edit| {
63
67
edit. replace (
64
68
method_call. syntax ( ) . text_range ( ) ,
65
- format ! ( "{}::from({})" , type_call_disp, receiver) ,
69
+ if sc. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == ':' ) {
70
+ format ! ( "{}::from({})" , sc, receiver)
71
+ } else {
72
+ format ! ( "<{}>::from({})" , sc, receiver)
73
+ } ,
66
74
) ;
67
75
} ,
68
76
) ;
@@ -199,6 +207,66 @@ mod C {
199
207
fn main() -> () {
200
208
let a: A = A;
201
209
let b: C::B = C::B::from(a);
210
+ }"# ,
211
+ )
212
+ }
213
+
214
+ #[ test]
215
+ fn preceding_type_qualifier ( ) {
216
+ check_assist (
217
+ into_to_qualified_from,
218
+ r#"
219
+ //- minicore: from
220
+ impl From<(i32,i32)> for [i32;2] {
221
+ fn from(value: (i32,i32)) -> Self {
222
+ [value.0, value.1]
223
+ }
224
+ }
225
+
226
+ fn tuple_to_array() -> [i32; 2] {
227
+ (0,1).in$0to()
228
+ }"# ,
229
+ r#"
230
+ impl From<(i32,i32)> for [i32;2] {
231
+ fn from(value: (i32,i32)) -> Self {
232
+ [value.0, value.1]
233
+ }
234
+ }
235
+
236
+ fn tuple_to_array() -> [i32; 2] {
237
+ <[i32; 2]>::from((0,1))
238
+ }"# ,
239
+ )
240
+ }
241
+
242
+ #[ test]
243
+ fn type_with_gens ( ) {
244
+ check_assist (
245
+ into_to_qualified_from,
246
+ r#"
247
+ //- minicore: from
248
+ struct StructA<Gen>(Gen);
249
+
250
+ impl From<i32> for StructA<i32> {
251
+ fn from(value: i32) -> Self {
252
+ StructA(value + 1)
253
+ }
254
+ }
255
+
256
+ fn main() -> () {
257
+ let a: StructA<i32> = 3.in$0to();
258
+ }"# ,
259
+ r#"
260
+ struct StructA<Gen>(Gen);
261
+
262
+ impl From<i32> for StructA<i32> {
263
+ fn from(value: i32) -> Self {
264
+ StructA(value + 1)
265
+ }
266
+ }
267
+
268
+ fn main() -> () {
269
+ let a: StructA<i32> = <StructA<i32>>::from(3);
202
270
}"# ,
203
271
)
204
272
}
0 commit comments