ITADN

Replace a single range in an `if let` by a boolean `if`

#17167Openronnodas 创建于 2026-06-06
A-lint
R
ronnodascommented
### What it does Matching against a single range pattern is equivalent to either an inequality or a `range.contains()` (ie two inequalities) or trivial. This lint would replace `let range = ...` in `if let` statements and let chains. ### Advantage - Easier to read - Range pattern matches only work with integers and chars, while inequalities work more generally, so changing the type of the variable being matched would require a smaller change once this lint is applied ### Drawbacks In const contexts and when the range is either `Range` or `RangeInclusive`, the lint may not be an improvement: - `Range::contains()` uses `PartialOrd` so cannot be used in const contexts - `if 3 <= x && x < 5` may be harder to read compared to `if let 3..5 = x` and if `x` is a complex expression then it requires a new binding to maintain the same semantics. ### Example ```rust if let 2.. = x && let ..10 = y && let 'a'..='z' = c { ... } ``` Could be written as: ```rust if x >= 2 && y < 10 && ('a'..='z').contains(&c) { ... } ``` ### Comparison with existing lints Instead of being a new lint, this could, or perhaps should, be part of `redundant_pattern_matching`. ### Additional Context _No response_
0 条评论