Omit QualPathTy when possible

This commit is contained in:
Ali Bektas 2023-09-22 13:21:38 +02:00
parent 695a1349fa
commit 132a6ce8fc
2 changed files with 15 additions and 13 deletions

View file

@ -36,7 +36,7 @@ use crate::assist_context::{AssistContext, Assists};
// //
// fn main() -> () { // fn main() -> () {
// let a = 3; // let a = 3;
// let b: B = <B>::from(a); // let b: B = B::from(a);
// } // }
// ``` // ```
pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
@ -54,22 +54,24 @@ pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>)
let type_call = sema.type_of_expr(&method_call.clone().into())?; let type_call = sema.type_of_expr(&method_call.clone().into())?;
let adjusted_tc = type_call.adjusted(); let adjusted_tc = type_call.adjusted();
if adjusted_tc.is_unknown() && adjusted_tc.contains_unknown() { if adjusted_tc.contains_unknown() {
return None; return None;
} }
let qualified_from = format!( let sc = adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?;
"<{}>::from({})",
adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?,
receiver
);
acc.add( acc.add(
AssistId("into_to_qualified_from", AssistKind::Generate), AssistId("into_to_qualified_from", AssistKind::Generate),
"Convert `into` to fully qualified `from`", "Convert `into` to fully qualified `from`",
nameref.syntax().text_range(), nameref.syntax().text_range(),
|edit| { |edit| {
edit.replace(method_call.syntax().text_range(), qualified_from); edit.replace(
method_call.syntax().text_range(),
if sc.chars().find(|c| !c.is_alphanumeric() && c != &':').is_some() {
format!("<{}>::from({})", sc, receiver)
} else {
format!("{}::from({})", sc, receiver)
},
);
}, },
); );
} }
@ -112,7 +114,7 @@ impl From<A> for B {
fn main() -> () { fn main() -> () {
let a: A = A; let a: A = A;
let b: B = <B>::from(a); let b: B = B::from(a);
}"#, }"#,
) )
} }
@ -160,7 +162,7 @@ mod C {
fn main() -> () { fn main() -> () {
let a: A = A; let a: A = A;
let b: B = <B>::from(a); let b: B = B::from(a);
}"#, }"#,
) )
} }
@ -204,7 +206,7 @@ mod C {
fn main() -> () { fn main() -> () {
let a: A = A; let a: A = A;
let b: C::B = <C::B>::from(a); let b: C::B = C::B::from(a);
}"#, }"#,
) )
} }

View file

@ -1812,7 +1812,7 @@ impl From<i32> for B {
fn main() -> () { fn main() -> () {
let a = 3; let a = 3;
let b: B = <B>::from(a); let b: B = B::from(a);
} }
"#####, "#####,
) )