diff --git a/crates/ide-assists/src/handlers/into_to_qualified_from.rs b/crates/ide-assists/src/handlers/into_to_qualified_from.rs index 663df266b6f..32d30e6b18d 100644 --- a/crates/ide-assists/src/handlers/into_to_qualified_from.rs +++ b/crates/ide-assists/src/handlers/into_to_qualified_from.rs @@ -52,18 +52,24 @@ pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>) == FamousDefs(sema, scope.krate()).core_convert_Into()? { let type_call = sema.type_of_expr(&method_call.clone().into())?; - let type_call_disp = - type_call.adjusted().display_source_code(db, scope.module().into(), true).ok()?; + let adjusted_tc = type_call.adjusted(); + + if adjusted_tc.is_unknown() && adjusted_tc.contains_unknown() { + return None; + } + + let qualified_from = format!( + "<{}>::from({})", + adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?, + receiver + ); acc.add( AssistId("into_to_qualified_from", AssistKind::Generate), "Convert `into` to fully qualified `from`", nameref.syntax().text_range(), |edit| { - edit.replace( - method_call.syntax().text_range(), - format!("{}::from({})", type_call_disp, receiver), - ); + edit.replace(method_call.syntax().text_range(), qualified_from); }, ); } @@ -106,7 +112,7 @@ impl From for B { fn main() -> () { let a: A = A; - let b: B = B::from(a); + let b: B = ::from(a); }"#, ) } @@ -154,7 +160,7 @@ mod C { fn main() -> () { let a: A = A; - let b: B = B::from(a); + let b: B = ::from(a); }"#, ) } @@ -198,7 +204,67 @@ mod C { fn main() -> () { let a: A = A; - let b: C::B = C::B::from(a); + let b: C::B = ::from(a); +}"#, + ) + } + + #[test] + fn preceding_type_qualifier() { + check_assist( + into_to_qualified_from, + r#" +//- minicore: from +impl From<(i32,i32)> for [i32;2] { + fn from(value: (i32,i32)) -> Self { + [value.0, value.1] + } +} + +fn tuple_to_array() -> [i32; 2] { + (0,1).in$0to() +}"#, + r#" +impl From<(i32,i32)> for [i32;2] { + fn from(value: (i32,i32)) -> Self { + [value.0, value.1] + } +} + +fn tuple_to_array() -> [i32; 2] { + <[i32; 2]>::from((0,1)) +}"#, + ) + } + + #[test] + fn type_with_gens() { + check_assist( + into_to_qualified_from, + r#" +//- minicore: from +struct StructA(Gen); + +impl From for StructA { + fn from(value: i32) -> Self { + StructA(value + 1) + } +} + +fn main() -> () { + let a: StructA = 3.in$0to(); +}"#, + r#" +struct StructA(Gen); + +impl From for StructA { + fn from(value: i32) -> Self { + StructA(value + 1) + } +} + +fn main() -> () { + let a: StructA = >::from(3); }"#, ) }