]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/matches/match_same_arms.rs
Auto merge of #105252 - bjorn3:codegen_less_pair_values, r=nagisa
[rust.git] / src / tools / clippy / clippy_lints / src / matches / match_same_arms.rs
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;
5 use core::iter;
6 use core::slice;
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;
13 use rustc_middle::ty;
14 use rustc_span::Symbol;
15
16 use super::MATCH_SAME_ARMS;
17
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);
23         h.finish()
24     };
25
26     let arena = DroplessArena::default();
27     let normalized_pats: Vec<_> = arms
28         .iter()
29         .map(|a| NormalizedPat::from_pat(cx, &arena, a.pat))
30         .collect();
31
32     // The furthest forwards a pattern can move without semantic changes
33     let forwards_blocking_idxs: Vec<_> = normalized_pats
34         .iter()
35         .enumerate()
36         .map(|(i, pat)| {
37             normalized_pats[i + 1..]
38                 .iter()
39                 .enumerate()
40                 .find_map(|(j, other)| pat.has_overlapping_values(other).then_some(i + 1 + j))
41                 .unwrap_or(normalized_pats.len())
42         })
43         .collect();
44
45     // The furthest backwards a pattern can move without semantic changes
46     let backwards_blocking_idxs: Vec<_> = normalized_pats
47         .iter()
48         .enumerate()
49         .map(|(i, pat)| {
50             normalized_pats[..i]
51                 .iter()
52                 .enumerate()
53                 .rev()
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)
58                 })
59                 .unwrap_or(0)
60         })
61         .collect();
62
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);
66
67         let mut local_map: HirIdMap<HirId> = HirIdMap::default();
68         let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
69             if_chain! {
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,
76                 };
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);
82                 then {
83                     entry.insert(b_id);
84                     true
85                 } else {
86                     false
87                 }
88             }
89         };
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())
101     };
102
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) {
106             span_lint_and_then(
107                 cx,
108                 MATCH_SAME_ARMS,
109                 arm1.span,
110                 "this match arm has an identical body to the `_` wildcard arm",
111                 |diag| {
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");
115                 },
116             );
117         } else {
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) {
120                 (arm1, arm2)
121             } else {
122                 (arm2, arm1)
123             };
124
125             span_lint_and_then(
126                 cx,
127                 MATCH_SAME_ARMS,
128                 keep_arm.span,
129                 "this match arm has an identical body to another arm",
130                 |diag| {
131                     let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
132                     let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
133
134                     diag.span_suggestion(
135                         keep_arm.pat.span,
136                         "try merging the arm patterns",
137                         format!("{keep_pat_snip} | {move_pat_snip}"),
138                         Applicability::MaybeIncorrect,
139                     )
140                     .help("or try changing either arm body")
141                     .span_note(move_arm.span, "other arm here");
142                 },
143             );
144         }
145     }
146 }
147
148 #[derive(Clone, Copy)]
149 enum NormalizedPat<'a> {
150     Wild,
151     Struct(Option<DefId>, &'a [(Symbol, Self)]),
152     Tuple(Option<DefId>, &'a [Self]),
153     Or(&'a [Self]),
154     Path(Option<DefId>),
155     LitStr(Symbol),
156     LitBytes(&'a [u8]),
157     LitInt(u128),
158     LitBool(bool),
159     Range(PatRange),
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
163     /// patterns.
164     Slice(&'a [Self], Option<&'a [Self]>),
165 }
166
167 #[derive(Clone, Copy)]
168 struct PatRange {
169     start: u128,
170     end: u128,
171     bounds: RangeEnd,
172 }
173 impl PatRange {
174     fn contains(&self, x: u128) -> bool {
175         x >= self.start
176             && match self.bounds {
177                 RangeEnd::Included => x <= self.end,
178                 RangeEnd::Excluded => x < self.end,
179             }
180     }
181
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.
185         (match self.bounds {
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,
191         })
192     }
193 }
194
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 {
200     struct Iter<'a>(
201         slice::Iter<'a, (Symbol, NormalizedPat<'a>)>,
202         slice::Iter<'a, (Symbol, NormalizedPat<'a>)>,
203     );
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()?;
210             loop {
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()?,
215                 }
216             }
217         }
218     }
219     Iter(left.iter(), right.iter())
220 }
221
222 #[expect(clippy::similar_names)]
223 impl<'a> NormalizedPat<'a> {
224     fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
225         match pat.kind {
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)
229             },
230             PatKind::Struct(ref path, fields, _) => {
231                 let 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)
235             },
236             PatKind::TupleStruct(ref path, pats, wild_idx) => {
237                 let Some(adt) = cx.typeck_results().pat_ty(pat).ty_adt_def() else {
238                     return Self::Wild
239                 };
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,
244                     }
245                 } else {
246                     (None, adt.non_enum_variant())
247                 };
248                 let (front, back) = match wild_idx.as_opt_usize() {
249                     Some(i) => pats.split_at(i),
250                     None => (pats, [].as_slice()),
251                 };
252                 let pats = arena.alloc_from_iter(
253                     front
254                         .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))),
258                 );
259                 Self::Tuple(var_id, pats)
260             },
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,
267                 };
268                 let (front, back) = match wild_idx.as_opt_usize() {
269                     Some(i) => pats.split_at(i),
270                     None => (pats, [].as_slice()),
271                 };
272                 let pats = arena.alloc_from_iter(
273                     front
274                         .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))),
278                 );
279                 Self::Tuple(None, pats)
280             },
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,
291                 },
292                 _ => Self::Wild,
293             },
294             PatKind::Range(start, end, bounds) => {
295                 // TODO: Handle negative integers. They're currently treated as a wild match.
296                 let start = match start {
297                     None => 0,
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,
304                         },
305                         _ => return Self::Wild,
306                     },
307                 };
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,
316                         },
317                         _ => return Self::Wild,
318                     },
319                 };
320                 Self::Range(PatRange { start, end, bounds })
321             },
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)))),
325             ),
326         }
327     }
328
329     /// Checks if two patterns overlap in the values they can match assuming they are for the same
330     /// type.
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))
336             },
337             (Self::Struct(lpath, lfields), Self::Struct(rpath, rfields)) => {
338                 if lpath != rpath {
339                     return false;
340                 }
341                 iter_matching_struct_fields(lfields, rfields).all(|(lpat, rpat)| lpat.has_overlapping_values(rpat))
342             },
343             (Self::Tuple(lpath, lpats), Self::Tuple(rpath, rpats)) => {
344                 if lpath != rpath {
345                     return false;
346                 }
347                 lpats
348                     .iter()
349                     .zip(rpats.iter())
350                     .all(|(lpat, rpat)| lpat.has_overlapping_values(rpat))
351             },
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))
361             },
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() {
367                     return false;
368                 }
369                 pats[..front.len()]
370                     .iter()
371                     .zip(front.iter())
372                     .chain(pats[pats.len() - back.len()..].iter().zip(back.iter()))
373                     .all(|(x, y)| x.has_overlapping_values(y))
374             },
375             (Self::Slice(lfront, Some(lback)), Self::Slice(rfront, Some(rback))) => lfront
376                 .iter()
377                 .zip(rfront.iter())
378                 .chain(lback.iter().rev().zip(rback.iter().rev()))
379                 .all(|(x, y)| x.has_overlapping_values(y)),
380
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,
384
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.
388                 x == y
389             },
390
391             // TODO: Lit* with Path, Range with Path, LitBytes with Slice
392             _ => true,
393         }
394     }
395 }
396
397 fn pat_contains_local(pat: &Pat<'_>, id: HirId) -> bool {
398     let mut result = false;
399     pat.walk_short(|p| {
400         result |= matches!(p.kind, PatKind::Binding(_, binding_id, ..) if binding_id == id);
401         !result
402     });
403     result
404 }
405
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()
411 }