mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 20:35:09 +00:00
Prefer match to if let else
This commit is contained in:
parent
d38fd77845
commit
30dea3a727
1 changed files with 21 additions and 0 deletions
|
@ -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).
|
**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
|
## Token names
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue