1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::{path_to_local, search_same, SpanlessEq, SpanlessHash};
4 use core::cmp::Ordering;
7 use rustc_arena::DroplessArena;
8 use rustc_ast::ast::LitKind;
9 use rustc_errors::Applicability;
10 use rustc_hir::def_id::DefId;
11 use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatKind, RangeEnd};
12 use rustc_lint::LateContext;
14 use rustc_span::Symbol;
16 use super::MATCH_SAME_ARMS;
18 #[expect(clippy::too_many_lines)]
19 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
20 let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 {
21 let mut h = SpanlessHash::new(cx);
22 h.hash_expr(arm.body);
26 let arena = DroplessArena::default();
27 let normalized_pats: Vec<_> = arms
29 .map(|a| NormalizedPat::from_pat(cx, &arena, a.pat))
32 // The furthest forwards a pattern can move without semantic changes
33 let forwards_blocking_idxs: Vec<_> = normalized_pats
37 normalized_pats[i + 1..]
40 .find_map(|(j, other)| pat.has_overlapping_values(other).then_some(i + 1 + j))
41 .unwrap_or(normalized_pats.len())
45 // The furthest backwards a pattern can move without semantic changes
46 let backwards_blocking_idxs: Vec<_> = normalized_pats
54 .zip(forwards_blocking_idxs[..i].iter().copied().rev())
55 .skip_while(|&(_, forward_block)| forward_block > i)
56 .find_map(|((j, other), forward_block)| {
57 (forward_block == i || pat.has_overlapping_values(other)).then_some(j)
63 let eq = |&(lindex, lhs): &(usize, &Arm<'_>), &(rindex, rhs): &(usize, &Arm<'_>)| -> bool {
64 let min_index = usize::min(lindex, rindex);
65 let max_index = usize::max(lindex, rindex);
67 let mut local_map: HirIdMap<HirId> = HirIdMap::default();
68 let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
70 if let Some(a_id) = path_to_local(a);
71 if let Some(b_id) = path_to_local(b);
72 let entry = match local_map.entry(a_id) {
73 HirIdMapEntry::Vacant(entry) => entry,
74 // check if using the same bindings as before
75 HirIdMapEntry::Occupied(entry) => return *entry.get() == b_id,
77 // the names technically don't have to match; this makes the lint more conservative
78 if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id);
79 if cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b);
80 if pat_contains_local(lhs.pat, a_id);
81 if pat_contains_local(rhs.pat, b_id);
90 // Arms with a guard are ignored, those can’t always be merged together
91 // If both arms overlap with an arm in between then these can't be merged either.
92 !(backwards_blocking_idxs[max_index] > min_index && forwards_blocking_idxs[min_index] < max_index)
93 && lhs.guard.is_none()
94 && rhs.guard.is_none()
95 && SpanlessEq::new(cx)
96 .expr_fallback(eq_fallback)
97 .eq_expr(lhs.body, rhs.body)
98 // these checks could be removed to allow unused bindings
99 && bindings_eq(lhs.pat, local_map.keys().copied().collect())
100 && bindings_eq(rhs.pat, local_map.values().copied().collect())
103 let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
104 for (&(i, arm1), &(j, arm2)) in search_same(&indexed_arms, hash, eq) {
105 if matches!(arm2.pat.kind, PatKind::Wild) {
110 "this match arm has an identical body to the `_` wildcard arm",
112 diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
113 .help("or try changing either arm body")
114 .span_note(arm2.span, "`_` wildcard arm here");
118 let back_block = backwards_blocking_idxs[j];
119 let (keep_arm, move_arm) = if back_block < i || (back_block == 0 && forwards_blocking_idxs[i] <= j) {
129 "this match arm has an identical body to another arm",
131 let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
132 let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
134 diag.span_suggestion(
136 "try merging the arm patterns",
137 format!("{keep_pat_snip} | {move_pat_snip}"),
138 Applicability::MaybeIncorrect,
140 .help("or try changing either arm body")
141 .span_note(move_arm.span, "other arm here");
148 #[derive(Clone, Copy)]
149 enum NormalizedPat<'a> {
151 Struct(Option<DefId>, &'a [(Symbol, Self)]),
152 Tuple(Option<DefId>, &'a [Self]),
160 /// A slice pattern. If the second value is `None`, then this matches an exact size. Otherwise
161 /// the first value contains everything before the `..` wildcard pattern, and the second value
162 /// contains everything afterwards. Note that either side, or both sides, may contain zero
164 Slice(&'a [Self], Option<&'a [Self]>),
167 #[derive(Clone, Copy)]
174 fn contains(&self, x: u128) -> bool {
176 && match self.bounds {
177 RangeEnd::Included => x <= self.end,
178 RangeEnd::Excluded => x < self.end,
182 fn overlaps(&self, other: &Self) -> bool {
183 // Note: Empty ranges are impossible, so this is correct even though it would return true if an
184 // empty exclusive range were to reside within an inclusive range.
186 RangeEnd::Included => self.end >= other.start,
187 RangeEnd::Excluded => self.end > other.start,
188 } && match other.bounds {
189 RangeEnd::Included => self.start <= other.end,
190 RangeEnd::Excluded => self.start < other.end,
195 /// Iterates over the pairs of fields with matching names.
196 fn iter_matching_struct_fields<'a>(
197 left: &'a [(Symbol, NormalizedPat<'a>)],
198 right: &'a [(Symbol, NormalizedPat<'a>)],
199 ) -> impl Iterator<Item = (&'a NormalizedPat<'a>, &'a NormalizedPat<'a>)> + 'a {
201 slice::Iter<'a, (Symbol, NormalizedPat<'a>)>,
202 slice::Iter<'a, (Symbol, NormalizedPat<'a>)>,
204 impl<'a> Iterator for Iter<'a> {
205 type Item = (&'a NormalizedPat<'a>, &'a NormalizedPat<'a>);
206 fn next(&mut self) -> Option<Self::Item> {
207 // Note: all the fields in each slice are sorted by symbol value.
208 let mut left = self.0.next()?;
209 let mut right = self.1.next()?;
211 match left.0.cmp(&right.0) {
212 Ordering::Equal => return Some((&left.1, &right.1)),
213 Ordering::Less => left = self.0.next()?,
214 Ordering::Greater => right = self.1.next()?,
219 Iter(left.iter(), right.iter())
222 #[expect(clippy::similar_names)]
223 impl<'a> NormalizedPat<'a> {
224 fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
226 PatKind::Wild | PatKind::Binding(.., None) => Self::Wild,
227 PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Ref(pat, _) => {
228 Self::from_pat(cx, arena, pat)
230 PatKind::Struct(ref path, fields, _) => {
232 arena.alloc_from_iter(fields.iter().map(|f| (f.ident.name, Self::from_pat(cx, arena, f.pat))));
233 fields.sort_by_key(|&(name, _)| name);
234 Self::Struct(cx.qpath_res(path, pat.hir_id).opt_def_id(), fields)
236 PatKind::TupleStruct(ref path, pats, wild_idx) => {
237 let Some(adt) = cx.typeck_results().pat_ty(pat).ty_adt_def() else {
240 let (var_id, variant) = if adt.is_enum() {
241 match cx.qpath_res(path, pat.hir_id).opt_def_id() {
242 Some(x) => (Some(x), adt.variant_with_ctor_id(x)),
243 None => return Self::Wild,
246 (None, adt.non_enum_variant())
248 let (front, back) = match wild_idx.as_opt_usize() {
249 Some(i) => pats.split_at(i),
250 None => (pats, [].as_slice()),
252 let pats = arena.alloc_from_iter(
255 .map(|pat| Self::from_pat(cx, arena, pat))
256 .chain(iter::repeat_with(|| Self::Wild).take(variant.fields.len() - pats.len()))
257 .chain(back.iter().map(|pat| Self::from_pat(cx, arena, pat))),
259 Self::Tuple(var_id, pats)
261 PatKind::Or(pats) => Self::Or(arena.alloc_from_iter(pats.iter().map(|pat| Self::from_pat(cx, arena, pat)))),
262 PatKind::Path(ref path) => Self::Path(cx.qpath_res(path, pat.hir_id).opt_def_id()),
263 PatKind::Tuple(pats, wild_idx) => {
264 let field_count = match cx.typeck_results().pat_ty(pat).kind() {
265 ty::Tuple(subs) => subs.len(),
266 _ => return Self::Wild,
268 let (front, back) = match wild_idx.as_opt_usize() {
269 Some(i) => pats.split_at(i),
270 None => (pats, [].as_slice()),
272 let pats = arena.alloc_from_iter(
275 .map(|pat| Self::from_pat(cx, arena, pat))
276 .chain(iter::repeat_with(|| Self::Wild).take(field_count - pats.len()))
277 .chain(back.iter().map(|pat| Self::from_pat(cx, arena, pat))),
279 Self::Tuple(None, pats)
281 PatKind::Lit(e) => match &e.kind {
282 // TODO: Handle negative integers. They're currently treated as a wild match.
283 ExprKind::Lit(lit) => match lit.node {
284 LitKind::Str(sym, _) => Self::LitStr(sym),
285 LitKind::ByteStr(ref bytes, _) => Self::LitBytes(bytes),
286 LitKind::Byte(val) => Self::LitInt(val.into()),
287 LitKind::Char(val) => Self::LitInt(val.into()),
288 LitKind::Int(val, _) => Self::LitInt(val),
289 LitKind::Bool(val) => Self::LitBool(val),
290 LitKind::Float(..) | LitKind::Err => Self::Wild,
294 PatKind::Range(start, end, bounds) => {
295 // TODO: Handle negative integers. They're currently treated as a wild match.
296 let start = match start {
298 Some(e) => match &e.kind {
299 ExprKind::Lit(lit) => match lit.node {
300 LitKind::Int(val, _) => val,
301 LitKind::Char(val) => val.into(),
302 LitKind::Byte(val) => val.into(),
303 _ => return Self::Wild,
305 _ => return Self::Wild,
308 let (end, bounds) = match end {
309 None => (u128::MAX, RangeEnd::Included),
310 Some(e) => match &e.kind {
311 ExprKind::Lit(lit) => match lit.node {
312 LitKind::Int(val, _) => (val, bounds),
313 LitKind::Char(val) => (val.into(), bounds),
314 LitKind::Byte(val) => (val.into(), bounds),
315 _ => return Self::Wild,
317 _ => return Self::Wild,
320 Self::Range(PatRange { start, end, bounds })
322 PatKind::Slice(front, wild_pat, back) => Self::Slice(
323 arena.alloc_from_iter(front.iter().map(|pat| Self::from_pat(cx, arena, pat))),
324 wild_pat.map(|_| &*arena.alloc_from_iter(back.iter().map(|pat| Self::from_pat(cx, arena, pat)))),
329 /// Checks if two patterns overlap in the values they can match assuming they are for the same
331 fn has_overlapping_values(&self, other: &Self) -> bool {
332 match (*self, *other) {
333 (Self::Wild, _) | (_, Self::Wild) => true,
334 (Self::Or(pats), ref other) | (ref other, Self::Or(pats)) => {
335 pats.iter().any(|pat| pat.has_overlapping_values(other))
337 (Self::Struct(lpath, lfields), Self::Struct(rpath, rfields)) => {
341 iter_matching_struct_fields(lfields, rfields).all(|(lpat, rpat)| lpat.has_overlapping_values(rpat))
343 (Self::Tuple(lpath, lpats), Self::Tuple(rpath, rpats)) => {
350 .all(|(lpat, rpat)| lpat.has_overlapping_values(rpat))
352 (Self::Path(x), Self::Path(y)) => x == y,
353 (Self::LitStr(x), Self::LitStr(y)) => x == y,
354 (Self::LitBytes(x), Self::LitBytes(y)) => x == y,
355 (Self::LitInt(x), Self::LitInt(y)) => x == y,
356 (Self::LitBool(x), Self::LitBool(y)) => x == y,
357 (Self::Range(ref x), Self::Range(ref y)) => x.overlaps(y),
358 (Self::Range(ref range), Self::LitInt(x)) | (Self::LitInt(x), Self::Range(ref range)) => range.contains(x),
359 (Self::Slice(lpats, None), Self::Slice(rpats, None)) => {
360 lpats.len() == rpats.len() && lpats.iter().zip(rpats.iter()).all(|(x, y)| x.has_overlapping_values(y))
362 (Self::Slice(pats, None), Self::Slice(front, Some(back)))
363 | (Self::Slice(front, Some(back)), Self::Slice(pats, None)) => {
364 // Here `pats` is an exact size match. If the combined lengths of `front` and `back` are greater
365 // then the minimum length required will be greater than the length of `pats`.
366 if pats.len() < front.len() + back.len() {
372 .chain(pats[pats.len() - back.len()..].iter().zip(back.iter()))
373 .all(|(x, y)| x.has_overlapping_values(y))
375 (Self::Slice(lfront, Some(lback)), Self::Slice(rfront, Some(rback))) => lfront
378 .chain(lback.iter().rev().zip(rback.iter().rev()))
379 .all(|(x, y)| x.has_overlapping_values(y)),
381 // Enums can mix unit variants with tuple/struct variants. These can never overlap.
382 (Self::Path(_), Self::Tuple(..) | Self::Struct(..))
383 | (Self::Tuple(..) | Self::Struct(..), Self::Path(_)) => false,
385 // Tuples can be matched like a struct.
386 (Self::Tuple(x, _), Self::Struct(y, _)) | (Self::Struct(x, _), Self::Tuple(y, _)) => {
387 // TODO: check fields here.
391 // TODO: Lit* with Path, Range with Path, LitBytes with Slice
397 fn pat_contains_local(pat: &Pat<'_>, id: HirId) -> bool {
398 let mut result = false;
400 result |= matches!(p.kind, PatKind::Binding(_, binding_id, ..) if binding_id == id);
406 /// Returns true if all the bindings in the `Pat` are in `ids` and vice versa
407 fn bindings_eq(pat: &Pat<'_>, mut ids: HirIdSet) -> bool {
408 let mut result = true;
409 pat.each_binding_or_first(&mut |_, id, _, _| result &= ids.remove(&id));
410 result && ids.is_empty()