Skip to content

Commit

Permalink
Auto merge of rust-lang#13311 - alex-semenyuk:fix_manual_range_patter…
Browse files Browse the repository at this point in the history
…ns, 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
  • Loading branch information
bors committed Aug 26, 2024
2 parents b3fc578 + 494112e commit f194e68
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/manual_range_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ impl Num {
impl LateLintPass<'_> for ManualRangePatterns {
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &'_ rustc_hir::Pat<'_>) {
// a pattern like 1 | 2 seems fine, lint if there are at least 3 alternatives
// or at least one range
// or more then one range (exclude triggering on stylistic using OR with one element
// like described https://github.com/rust-lang/rust-clippy/issues/11825)
if let PatKind::Or(pats) = pat.kind
&& (pats.len() >= 3 || pats.iter().any(|p| matches!(p.kind, PatKind::Range(..))))
&& (pats.len() >= 3 || (pats.len() > 1 && pats.iter().any(|p| matches!(p.kind, PatKind::Range(..)))))
&& !in_external_macro(cx.sess(), pat.span)
{
let mut min = Num::dummy(i128::MAX);
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/manual_range_patterns.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ fn main() {
};
}
mac!(f);

#[rustfmt::skip]
let _ = match f {
| 2..=15 => 4,
| 241..=254 => 5,
| 255 => 6,
| _ => 7,
};
}
8 changes: 8 additions & 0 deletions tests/ui/manual_range_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ fn main() {
};
}
mac!(f);

#[rustfmt::skip]
let _ = match f {
| 2..=15 => 4,
| 241..=254 => 5,
| 255 => 6,
| _ => 7,
};
}

0 comments on commit f194e68

Please sign in to comment.