@@ -52,17 +52,25 @@ pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>)
5252 == FamousDefs ( sema, scope. krate ( ) ) . core_convert_Into ( ) ?
5353 {
5454 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 ( ) ;
5756
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 ( ) ?;
5862 acc. add (
5963 AssistId ( "into_to_qualified_from" , AssistKind :: Generate ) ,
6064 "Convert `into` to fully qualified `from`" ,
6165 nameref. syntax ( ) . text_range ( ) ,
6266 |edit| {
6367 edit. replace (
6468 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+ } ,
6674 ) ;
6775 } ,
6876 ) ;
@@ -199,6 +207,66 @@ mod C {
199207fn main() -> () {
200208 let a: A = A;
201209 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);
202270}"# ,
203271 )
204272 }
0 commit comments