diff options
author | Aleksey Kladov <[email protected]> | 2021-03-16 07:51:05 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-03-16 07:51:05 +0000 |
commit | 30dea3a727879f9ed6cfe06433f1adb51765fa9e (patch) | |
tree | 5149759011cd3b4f034d5b36b3797aa59c722be2 /docs/dev | |
parent | d38fd77845c40c6f07507c5c436af903a452efbd (diff) |
Prefer match to if let else
Diffstat (limited to 'docs/dev')
-rw-r--r-- | docs/dev/style.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index c4eb7bc7a..e4a1672ca 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -787,6 +787,27 @@ assert!(0 > x); | |||
787 | 787 | ||
788 | **Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line). | 788 | **Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line). |
789 | 789 | ||
790 | ## If-let | ||
791 | |||
792 | Avoid `if let ... { } else { }` construct, use `match` instead. | ||
793 | |||
794 | ```rust | ||
795 | // GOOD | ||
796 | match ctx.expected_type.as_ref() { | ||
797 | Some(expected_type) => completion_ty == expected_type && !expected_type.is_unit(), | ||
798 | None => false, | ||
799 | } | ||
800 | |||
801 | // BAD | ||
802 | if let Some(expected_type) = ctx.expected_type.as_ref() { | ||
803 | completion_ty == expected_type && !expected_type.is_unit() | ||
804 | } else { | ||
805 | false | ||
806 | } | ||
807 | ``` | ||
808 | |||
809 | **Rational:** `match` is almost always more compact. | ||
810 | The `else` branch can get a more precise pattern: `None` or `Err(_)` instead of `_`. | ||
790 | 811 | ||
791 | ## Token names | 812 | ## Token names |
792 | 813 | ||