diff options
| author | Ali Bektas <bektasali@protonmail.com> | 2023-09-22 13:21:38 +0200 |
|---|---|---|
| committer | Ali Bektas <bektasali@protonmail.com> | 2023-09-22 14:04:17 +0200 |
| commit | 132a6ce8fc20ccf56d29334fecf978a9ceec2a55 (patch) | |
| tree | 0e8e7b27b6c68d17c0ca7ad57424f55c3ebd8148 | |
| parent | 695a1349fa1c41333e079381e4ec06bb6539f2f1 (diff) | |
| download | rust-132a6ce8fc20ccf56d29334fecf978a9ceec2a55.tar.gz rust-132a6ce8fc20ccf56d29334fecf978a9ceec2a55.zip | |
Omit QualPathTy when possible
| -rw-r--r-- | crates/ide-assists/src/handlers/into_to_qualified_from.rs | 26 | ||||
| -rw-r--r-- | crates/ide-assists/src/tests/generated.rs | 2 |
2 files changed, 15 insertions, 13 deletions
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 640883b1bc1..0589cbaf8a7 100644 --- a/crates/ide-assists/src/handlers/into_to_qualified_from.rs +++ b/crates/ide-assists/src/handlers/into_to_qualified_from.rs @@ -36,7 +36,7 @@ use crate::assist_context::{AssistContext, Assists}; // // fn main() -> () { // 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<()> { @@ -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 adjusted_tc = type_call.adjusted(); - if adjusted_tc.is_unknown() && adjusted_tc.contains_unknown() { + if adjusted_tc.contains_unknown() { return None; } - let qualified_from = format!( - "<{}>::from({})", - adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?, - receiver - ); - + let sc = adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?; 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(), 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() -> () { let a: A = A; - let b: B = <B>::from(a); + let b: B = B::from(a); }"#, ) } @@ -160,7 +162,7 @@ mod C { fn main() -> () { let a: A = A; - let b: B = <B>::from(a); + let b: B = B::from(a); }"#, ) } @@ -204,7 +206,7 @@ mod C { fn main() -> () { let a: A = A; - let b: C::B = <C::B>::from(a); + let b: C::B = C::B::from(a); }"#, ) } diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index e65058f70b5..63a08a0e569 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -1812,7 +1812,7 @@ impl From<i32> for B { fn main() -> () { let a = 3; - let b: B = <B>::from(a); + let b: B = B::from(a); } "#####, ) |
