From 494112e51f7a2b59866c835810c3e87a10912c0a Mon Sep 17 00:00:00 2001 From: Alexey Semenyuk Date: Mon, 26 Aug 2024 20:01:29 +0500 Subject: [PATCH] Fix manual_range_patterns case with one element at OR --- clippy_lints/src/manual_range_patterns.rs | 5 +++-- tests/ui/manual_range_patterns.fixed | 8 ++++++++ tests/ui/manual_range_patterns.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/manual_range_patterns.rs b/clippy_lints/src/manual_range_patterns.rs index 9afb2b5bc84..66d2ab6b24a 100644 --- a/clippy_lints/src/manual_range_patterns.rs +++ b/clippy_lints/src/manual_range_patterns.rs @@ -77,9 +77,10 @@ fn min(self, other: Self) -> Self { 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); diff --git a/tests/ui/manual_range_patterns.fixed b/tests/ui/manual_range_patterns.fixed index f1b99637afd..60467bf9e88 100644 --- a/tests/ui/manual_range_patterns.fixed +++ b/tests/ui/manual_range_patterns.fixed @@ -45,4 +45,12 @@ fn main() { }; } mac!(f); + + #[rustfmt::skip] + let _ = match f { + | 2..=15 => 4, + | 241..=254 => 5, + | 255 => 6, + | _ => 7, + }; } diff --git a/tests/ui/manual_range_patterns.rs b/tests/ui/manual_range_patterns.rs index 869ffbe80b9..9cd80333449 100644 --- a/tests/ui/manual_range_patterns.rs +++ b/tests/ui/manual_range_patterns.rs @@ -45,4 +45,12 @@ macro_rules! mac { }; } mac!(f); + + #[rustfmt::skip] + let _ = match f { + | 2..=15 => 4, + | 241..=254 => 5, + | 255 => 6, + | _ => 7, + }; }