diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-27 14:29:10 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-27 14:29:10 +0000 |
commit | 4a24edd989720f1232d8f6a660d790ae77115964 (patch) | |
tree | 6efdc9d6ce4a1c9e8c886c1b3e70a17d6a19fe7b /crates/ide_assists/src/tests | |
parent | 2a4076c14d0e3f7ae03908c2b9cd1a52851d401c (diff) | |
parent | 558bcf4e0bf9d94ab51238e59f6fc5c170f38c3e (diff) |
Merge #7677
7677: More enum matching r=yoshuawuyts a=jDomantas
* Renamed existing `generate_enum_match_method` to `generate_enum_is_variant`
* Added two similar assists to generate `into_` and `as_` methods.
* Made all of them general enough to work on record and tuple variants too.
For `as_` method generation there's room to improve:
* Right now it always returns `Option<&Field>`, even though `Option<Field>` would be nicer when `Field: Copy`. I don't know how to check if the field type implements `Copy`. If given suggestions I could try to fix this in a follow-up pr.
* `&String` could be replaced with `&str`, `&Box<_>` with `&_`, and probably some more. I don't know what would be a good way to do that.
Closes #7604
Co-authored-by: Domantas Jadenkus <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/tests')
-rw-r--r-- | crates/ide_assists/src/tests/generated.rs | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index d42875822..7f6dbbccf 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs | |||
@@ -483,9 +483,38 @@ struct Point { | |||
483 | } | 483 | } |
484 | 484 | ||
485 | #[test] | 485 | #[test] |
486 | fn doctest_generate_enum_match_method() { | 486 | fn doctest_generate_enum_as_method() { |
487 | check_doc_test( | 487 | check_doc_test( |
488 | "generate_enum_match_method", | 488 | "generate_enum_as_method", |
489 | r#####" | ||
490 | enum Value { | ||
491 | Number(i32), | ||
492 | Text(String)$0, | ||
493 | } | ||
494 | "#####, | ||
495 | r#####" | ||
496 | enum Value { | ||
497 | Number(i32), | ||
498 | Text(String), | ||
499 | } | ||
500 | |||
501 | impl Value { | ||
502 | fn as_text(&self) -> Option<&String> { | ||
503 | if let Self::Text(v) = self { | ||
504 | Some(v) | ||
505 | } else { | ||
506 | None | ||
507 | } | ||
508 | } | ||
509 | } | ||
510 | "#####, | ||
511 | ) | ||
512 | } | ||
513 | |||
514 | #[test] | ||
515 | fn doctest_generate_enum_is_method() { | ||
516 | check_doc_test( | ||
517 | "generate_enum_is_method", | ||
489 | r#####" | 518 | r#####" |
490 | enum Version { | 519 | enum Version { |
491 | Undefined, | 520 | Undefined, |
@@ -511,6 +540,35 @@ impl Version { | |||
511 | } | 540 | } |
512 | 541 | ||
513 | #[test] | 542 | #[test] |
543 | fn doctest_generate_enum_try_into_method() { | ||
544 | check_doc_test( | ||
545 | "generate_enum_try_into_method", | ||
546 | r#####" | ||
547 | enum Value { | ||
548 | Number(i32), | ||
549 | Text(String)$0, | ||
550 | } | ||
551 | "#####, | ||
552 | r#####" | ||
553 | enum Value { | ||
554 | Number(i32), | ||
555 | Text(String), | ||
556 | } | ||
557 | |||
558 | impl Value { | ||
559 | fn try_into_text(self) -> Result<String, Self> { | ||
560 | if let Self::Text(v) = self { | ||
561 | Ok(v) | ||
562 | } else { | ||
563 | Err(self) | ||
564 | } | ||
565 | } | ||
566 | } | ||
567 | "#####, | ||
568 | ) | ||
569 | } | ||
570 | |||
571 | #[test] | ||
514 | fn doctest_generate_from_impl_for_enum() { | 572 | fn doctest_generate_from_impl_for_enum() { |
515 | check_doc_test( | 573 | check_doc_test( |
516 | "generate_from_impl_for_enum", | 574 | "generate_from_impl_for_enum", |