Skip to content

Commit f194e68

Browse files
committed
Auto merge of rust-lang#13311 - alex-semenyuk:fix_manual_range_patterns, r=Manishearth
Fix manual_range_patterns case with one element at OR Close rust-lang#11825 As mentioned rust-lang#11825 `OR` can be used for stylistic purposes with one element, we can filter this case from triggering lint changelog: [`manual_range_patterns`]: not trigger when `OR` has only one element
2 parents b3fc578 + 494112e commit f194e68

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clippy_lints/src/manual_range_patterns.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ impl Num {
7777
impl LateLintPass<'_> for ManualRangePatterns {
7878
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &'_ rustc_hir::Pat<'_>) {
7979
// a pattern like 1 | 2 seems fine, lint if there are at least 3 alternatives
80-
// or at least one range
80+
// or more then one range (exclude triggering on stylistic using OR with one element
81+
// like described https://github.com/rust-lang/rust-clippy/issues/11825)
8182
if let PatKind::Or(pats) = pat.kind
82-
&& (pats.len() >= 3 || pats.iter().any(|p| matches!(p.kind, PatKind::Range(..))))
83+
&& (pats.len() >= 3 || (pats.len() > 1 && pats.iter().any(|p| matches!(p.kind, PatKind::Range(..)))))
8384
&& !in_external_macro(cx.sess(), pat.span)
8485
{
8586
let mut min = Num::dummy(i128::MAX);

tests/ui/manual_range_patterns.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ fn main() {
4545
};
4646
}
4747
mac!(f);
48+
49+
#[rustfmt::skip]
50+
let _ = match f {
51+
| 2..=15 => 4,
52+
| 241..=254 => 5,
53+
| 255 => 6,
54+
| _ => 7,
55+
};
4856
}

tests/ui/manual_range_patterns.rs

+8
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ fn main() {
4545
};
4646
}
4747
mac!(f);
48+
49+
#[rustfmt::skip]
50+
let _ = match f {
51+
| 2..=15 => 4,
52+
| 241..=254 => 5,
53+
| 255 => 6,
54+
| _ => 7,
55+
};
4856
}

0 commit comments

Comments
 (0)