2 helpers::{mod_path_to_ast, FamousDefs},
3 traits::resolve_target_trait,
5 use syntax::ast::{self, AstNode, HasName};
7 use crate::{AssistContext, AssistId, AssistKind, Assists};
9 // FIXME: this should be a diagnostic
11 // Assist: convert_into_to_from
13 // Converts an Into impl to an equivalent From impl.
16 // # //- minicore: from
17 // impl $0Into<Thing> for usize {
18 // fn into(self) -> Thing {
20 // b: self.to_string(),
28 // impl From<usize> for Thing {
29 // fn from(val: usize) -> Self {
31 // b: val.to_string(),
37 pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let impl_ = ctx.find_node_at_offset::<ast::Impl>()?;
39 let src_type = impl_.self_ty()?;
40 let ast_trait = impl_.trait_()?;
42 let module = ctx.sema.scope(impl_.syntax()).module()?;
44 let trait_ = resolve_target_trait(&ctx.sema, &impl_)?;
45 if trait_ != FamousDefs(&ctx.sema, Some(module.krate())).core_convert_Into()? {
50 let src_type_path = src_type.syntax().descendants().find_map(ast::Path::cast)?;
51 let src_type_def = match ctx.sema.resolve_path(&src_type_path) {
52 Some(hir::PathResolution::Def(module_def)) => module_def,
56 mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
59 let dest_type = match &ast_trait {
60 ast::Type::PathType(path) => {
61 path.path()?.segment()?.generic_arg_list()?.generic_args().next()?
66 let into_fn = impl_.assoc_item_list()?.assoc_items().find_map(|item| {
67 if let ast::AssocItem::Fn(f) = item {
68 if f.name()?.text() == "into" {
75 let into_fn_name = into_fn.name()?;
76 let into_fn_params = into_fn.param_list()?;
77 let into_fn_return = into_fn.ret_type()?;
83 .filter_map(ast::NameRef::cast)
84 .filter(|name| name.text() == "self" || name.text() == "Self");
87 AssistId("convert_into_to_from", AssistKind::RefactorRewrite),
88 "Convert Into to From",
89 impl_.syntax().text_range(),
91 builder.replace(src_type.syntax().text_range(), dest_type.to_string());
92 builder.replace(ast_trait.syntax().text_range(), format!("From<{}>", src_type));
93 builder.replace(into_fn_return.syntax().text_range(), "-> Self");
94 builder.replace(into_fn_params.syntax().text_range(), format!("(val: {})", src_type));
95 builder.replace(into_fn_name.syntax().text_range(), "from");
98 match s.text().as_ref() {
99 "self" => builder.replace(s.syntax().text_range(), "val"),
100 "Self" => builder.replace(s.syntax().text_range(), src_type_path.to_string()),
112 use crate::tests::{check_assist, check_assist_not_applicable};
115 fn convert_into_to_from_converts_a_struct() {
117 convert_into_to_from,
125 impl $0core::convert::Into<Thing> for usize {
126 fn into(self) -> Thing {
140 impl From<usize> for Thing {
141 fn from(val: usize) -> Self {
153 fn convert_into_to_from_converts_enums() {
155 convert_into_to_from,
163 impl $0core::convert::Into<String> for Thing {
164 fn into(self) -> String {
178 impl From<Thing> for String {
179 fn from(val: Thing) -> Self {
191 fn convert_into_to_from_on_enum_with_lifetimes() {
193 convert_into_to_from,
201 impl<'a> $0core::convert::Into<&'a str> for Thing<'a> {
202 fn into(self) -> &'a str {
216 impl<'a> From<Thing<'a>> for &'a str {
217 fn from(val: Thing<'a>) -> Self {
229 fn convert_into_to_from_works_on_references() {
231 convert_into_to_from,
234 struct Thing(String);
236 impl $0core::convert::Into<String> for &Thing {
237 fn into(self) -> Thing {
243 struct Thing(String);
245 impl From<&Thing> for String {
246 fn from(val: &Thing) -> Self {
255 fn convert_into_to_from_works_on_qualified_structs() {
257 convert_into_to_from,
261 pub struct Thing(String);
262 pub struct BetterThing(String);
265 impl $0core::convert::Into<things::BetterThing> for &things::Thing {
266 fn into(self) -> Thing {
267 things::BetterThing(self.0.clone())
273 pub struct Thing(String);
274 pub struct BetterThing(String);
277 impl From<&things::Thing> for things::BetterThing {
278 fn from(val: &things::Thing) -> Self {
279 things::BetterThing(val.0.clone())
287 fn convert_into_to_from_works_on_qualified_enums() {
289 convert_into_to_from,
296 pub struct BetterThing {
301 impl $0core::convert::Into<things::BetterThing> for &things::Thing {
302 fn into(self) -> Thing {
304 Self::A(s) => things::BetterThing::B(s)
314 pub struct BetterThing {
319 impl From<&things::Thing> for things::BetterThing {
320 fn from(val: &things::Thing) -> Self {
322 things::Thing::A(s) => things::BetterThing::B(s)
331 fn convert_into_to_from_not_applicable_on_any_trait_named_into() {
332 check_assist_not_applicable(
333 convert_into_to_from,
337 pub fn into(self) -> T;
344 impl $0Into<Thing> for String {
345 fn into(self) -> Thing {