From 30dea3a727879f9ed6cfe06433f1adb51765fa9e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 16 Mar 2021 10:51:05 +0300 Subject: Prefer match to if let else --- docs/dev/style.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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); **Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line). +## If-let + +Avoid `if let ... { } else { }` construct, use `match` instead. + +```rust +// GOOD +match ctx.expected_type.as_ref() { + Some(expected_type) => completion_ty == expected_type && !expected_type.is_unit(), + None => false, +} + +// BAD +if let Some(expected_type) = ctx.expected_type.as_ref() { + completion_ty == expected_type && !expected_type.is_unit() +} else { + false +} +``` + +**Rational:** `match` is almost always more compact. +The `else` branch can get a more precise pattern: `None` or `Err(_)` instead of `_`. ## Token names -- cgit v1.2.3