diff options
Diffstat (limited to 'crates/ide_assists/src/handlers/convert_into_to_from.rs')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_into_to_from.rs | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/crates/ide_assists/src/handlers/convert_into_to_from.rs b/crates/ide_assists/src/handlers/convert_into_to_from.rs index 199e1ad5c..79a0c4879 100644 --- a/crates/ide_assists/src/handlers/convert_into_to_from.rs +++ b/crates/ide_assists/src/handlers/convert_into_to_from.rs | |||
@@ -6,6 +6,8 @@ use syntax::ast::{self, AstNode, NameOwner}; | |||
6 | 6 | ||
7 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 7 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
8 | 8 | ||
9 | // FIXME: this should be a diagnostic | ||
10 | |||
9 | // Assist: convert_into_to_from | 11 | // Assist: convert_into_to_from |
10 | // | 12 | // |
11 | // Converts an Into impl to an equivalent From impl. | 13 | // Converts an Into impl to an equivalent From impl. |
@@ -114,12 +116,14 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext) -> Op | |||
114 | mod tests { | 116 | mod tests { |
115 | use super::*; | 117 | use super::*; |
116 | 118 | ||
117 | use crate::tests::check_assist; | 119 | use crate::tests::{check_assist, check_assist_not_applicable}; |
118 | 120 | ||
119 | #[test] | 121 | #[test] |
120 | fn convert_into_to_from_converts_a_struct() { | 122 | fn convert_into_to_from_converts_a_struct() { |
121 | check_convert_into_to_from( | 123 | check_assist( |
124 | convert_into_to_from, | ||
122 | r#" | 125 | r#" |
126 | //- minicore: from | ||
123 | struct Thing { | 127 | struct Thing { |
124 | a: String, | 128 | a: String, |
125 | b: usize | 129 | b: usize |
@@ -154,8 +158,10 @@ impl From<usize> for Thing { | |||
154 | 158 | ||
155 | #[test] | 159 | #[test] |
156 | fn convert_into_to_from_converts_enums() { | 160 | fn convert_into_to_from_converts_enums() { |
157 | check_convert_into_to_from( | 161 | check_assist( |
162 | convert_into_to_from, | ||
158 | r#" | 163 | r#" |
164 | //- minicore: from | ||
159 | enum Thing { | 165 | enum Thing { |
160 | Foo(String), | 166 | Foo(String), |
161 | Bar(String) | 167 | Bar(String) |
@@ -190,8 +196,10 @@ impl From<Thing> for String { | |||
190 | 196 | ||
191 | #[test] | 197 | #[test] |
192 | fn convert_into_to_from_on_enum_with_lifetimes() { | 198 | fn convert_into_to_from_on_enum_with_lifetimes() { |
193 | check_convert_into_to_from( | 199 | check_assist( |
200 | convert_into_to_from, | ||
194 | r#" | 201 | r#" |
202 | //- minicore: from | ||
195 | enum Thing<'a> { | 203 | enum Thing<'a> { |
196 | Foo(&'a str), | 204 | Foo(&'a str), |
197 | Bar(&'a str) | 205 | Bar(&'a str) |
@@ -226,8 +234,10 @@ impl<'a> From<Thing<'a>> for &'a str { | |||
226 | 234 | ||
227 | #[test] | 235 | #[test] |
228 | fn convert_into_to_from_works_on_references() { | 236 | fn convert_into_to_from_works_on_references() { |
229 | check_convert_into_to_from( | 237 | check_assist( |
238 | convert_into_to_from, | ||
230 | r#" | 239 | r#" |
240 | //- minicore: from | ||
231 | struct Thing(String); | 241 | struct Thing(String); |
232 | 242 | ||
233 | impl $0core::convert::Into<String> for &Thing { | 243 | impl $0core::convert::Into<String> for &Thing { |
@@ -250,8 +260,10 @@ impl From<&Thing> for String { | |||
250 | 260 | ||
251 | #[test] | 261 | #[test] |
252 | fn convert_into_to_from_works_on_qualified_structs() { | 262 | fn convert_into_to_from_works_on_qualified_structs() { |
253 | check_convert_into_to_from( | 263 | check_assist( |
264 | convert_into_to_from, | ||
254 | r#" | 265 | r#" |
266 | //- minicore: from | ||
255 | mod things { | 267 | mod things { |
256 | pub struct Thing(String); | 268 | pub struct Thing(String); |
257 | pub struct BetterThing(String); | 269 | pub struct BetterThing(String); |
@@ -280,8 +292,10 @@ impl From<&things::Thing> for things::BetterThing { | |||
280 | 292 | ||
281 | #[test] | 293 | #[test] |
282 | fn convert_into_to_from_works_on_qualified_enums() { | 294 | fn convert_into_to_from_works_on_qualified_enums() { |
283 | check_convert_into_to_from( | 295 | check_assist( |
296 | convert_into_to_from, | ||
284 | r#" | 297 | r#" |
298 | //- minicore: from | ||
285 | mod things { | 299 | mod things { |
286 | pub enum Thing { | 300 | pub enum Thing { |
287 | A(String) | 301 | A(String) |
@@ -323,10 +337,12 @@ impl From<&things::Thing> for things::BetterThing { | |||
323 | #[test] | 337 | #[test] |
324 | fn convert_into_to_from_not_applicable_on_any_trait_named_into() { | 338 | fn convert_into_to_from_not_applicable_on_any_trait_named_into() { |
325 | check_assist_not_applicable( | 339 | check_assist_not_applicable( |
340 | convert_into_to_from, | ||
326 | r#" | 341 | r#" |
327 | pub trait Into<T> {{ | 342 | //- minicore: from |
343 | pub trait Into<T> { | ||
328 | pub fn into(self) -> T; | 344 | pub fn into(self) -> T; |
329 | }} | 345 | } |
330 | 346 | ||
331 | struct Thing { | 347 | struct Thing { |
332 | a: String, | 348 | a: String, |
@@ -342,14 +358,4 @@ impl $0Into<Thing> for String { | |||
342 | "#, | 358 | "#, |
343 | ); | 359 | ); |
344 | } | 360 | } |
345 | |||
346 | fn check_convert_into_to_from(before: &str, after: &str) { | ||
347 | let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); | ||
348 | check_assist(convert_into_to_from, before, after); | ||
349 | } | ||
350 | |||
351 | fn check_assist_not_applicable(before: &str) { | ||
352 | let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); | ||
353 | crate::tests::check_assist_not_applicable(convert_into_to_from, before); | ||
354 | } | ||
355 | } | 361 | } |