Skip to content

Commit 132a6ce

Browse files
committed
Omit QualPathTy when possible
1 parent 695a134 commit 132a6ce

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

crates/ide-assists/src/handlers/into_to_qualified_from.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::assist_context::{AssistContext, Assists};
3636
//
3737
// fn main() -> () {
3838
// let a = 3;
39-
// let b: B = <B>::from(a);
39+
// let b: B = B::from(a);
4040
// }
4141
// ```
4242
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<'_>)
5454
let type_call = sema.type_of_expr(&method_call.clone().into())?;
5555
let adjusted_tc = type_call.adjusted();
5656

57-
if adjusted_tc.is_unknown() && adjusted_tc.contains_unknown() {
57+
if adjusted_tc.contains_unknown() {
5858
return None;
5959
}
6060

61-
let qualified_from = format!(
62-
"<{}>::from({})",
63-
adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?,
64-
receiver
65-
);
66-
61+
let sc = adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?;
6762
acc.add(
6863
AssistId("into_to_qualified_from", AssistKind::Generate),
6964
"Convert `into` to fully qualified `from`",
7065
nameref.syntax().text_range(),
7166
|edit| {
72-
edit.replace(method_call.syntax().text_range(), qualified_from);
67+
edit.replace(
68+
method_call.syntax().text_range(),
69+
if sc.chars().find(|c| !c.is_alphanumeric() && c != &':').is_some() {
70+
format!("<{}>::from({})", sc, receiver)
71+
} else {
72+
format!("{}::from({})", sc, receiver)
73+
},
74+
);
7375
},
7476
);
7577
}
@@ -112,7 +114,7 @@ impl From<A> for B {
112114
113115
fn main() -> () {
114116
let a: A = A;
115-
let b: B = <B>::from(a);
117+
let b: B = B::from(a);
116118
}"#,
117119
)
118120
}
@@ -160,7 +162,7 @@ mod C {
160162
161163
fn main() -> () {
162164
let a: A = A;
163-
let b: B = <B>::from(a);
165+
let b: B = B::from(a);
164166
}"#,
165167
)
166168
}
@@ -204,7 +206,7 @@ mod C {
204206
205207
fn main() -> () {
206208
let a: A = A;
207-
let b: C::B = <C::B>::from(a);
209+
let b: C::B = C::B::from(a);
208210
}"#,
209211
)
210212
}

crates/ide-assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ impl From<i32> for B {
18121812
18131813
fn main() -> () {
18141814
let a = 3;
1815-
let b: B = <B>::from(a);
1815+
let b: B = B::from(a);
18161816
}
18171817
"#####,
18181818
)

0 commit comments

Comments
 (0)