|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then;
|
2 | 2 | use clippy_utils::source::snippet;
|
3 | 3 | use clippy_utils::{path_to_local, search_same, SpanlessEq, SpanlessHash};
|
4 |
| -use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdSet, MatchSource, Pat, PatKind}; |
| 4 | +use rustc_hir::{Arm, Expr, HirId, HirIdMap, HirIdSet, Pat, PatKind}; |
5 | 5 | use rustc_lint::LateContext;
|
6 | 6 | use std::collections::hash_map::Entry;
|
7 | 7 |
|
8 | 8 | use super::MATCH_SAME_ARMS;
|
9 | 9 |
|
10 |
| -pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) { |
11 |
| - if let ExprKind::Match(_, arms, MatchSource::Normal) = expr.kind { |
12 |
| - let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 { |
13 |
| - let mut h = SpanlessHash::new(cx); |
14 |
| - h.hash_expr(arm.body); |
15 |
| - h.finish() |
16 |
| - }; |
| 10 | +pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) { |
| 11 | + let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 { |
| 12 | + let mut h = SpanlessHash::new(cx); |
| 13 | + h.hash_expr(arm.body); |
| 14 | + h.finish() |
| 15 | + }; |
17 | 16 |
|
18 |
| - let eq = |&(lindex, lhs): &(usize, &Arm<'_>), &(rindex, rhs): &(usize, &Arm<'_>)| -> bool { |
19 |
| - let min_index = usize::min(lindex, rindex); |
20 |
| - let max_index = usize::max(lindex, rindex); |
| 17 | + let eq = |&(lindex, lhs): &(usize, &Arm<'_>), &(rindex, rhs): &(usize, &Arm<'_>)| -> bool { |
| 18 | + let min_index = usize::min(lindex, rindex); |
| 19 | + let max_index = usize::max(lindex, rindex); |
21 | 20 |
|
22 |
| - let mut local_map: HirIdMap<HirId> = HirIdMap::default(); |
23 |
| - let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| { |
24 |
| - if_chain! { |
25 |
| - if let Some(a_id) = path_to_local(a); |
26 |
| - if let Some(b_id) = path_to_local(b); |
27 |
| - let entry = match local_map.entry(a_id) { |
28 |
| - Entry::Vacant(entry) => entry, |
29 |
| - // check if using the same bindings as before |
30 |
| - Entry::Occupied(entry) => return *entry.get() == b_id, |
31 |
| - }; |
32 |
| - // the names technically don't have to match; this makes the lint more conservative |
33 |
| - if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id); |
34 |
| - if cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b); |
35 |
| - if pat_contains_local(lhs.pat, a_id); |
36 |
| - if pat_contains_local(rhs.pat, b_id); |
37 |
| - then { |
38 |
| - entry.insert(b_id); |
39 |
| - true |
40 |
| - } else { |
41 |
| - false |
42 |
| - } |
| 21 | + let mut local_map: HirIdMap<HirId> = HirIdMap::default(); |
| 22 | + let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| { |
| 23 | + if_chain! { |
| 24 | + if let Some(a_id) = path_to_local(a); |
| 25 | + if let Some(b_id) = path_to_local(b); |
| 26 | + let entry = match local_map.entry(a_id) { |
| 27 | + Entry::Vacant(entry) => entry, |
| 28 | + // check if using the same bindings as before |
| 29 | + Entry::Occupied(entry) => return *entry.get() == b_id, |
| 30 | + }; |
| 31 | + // the names technically don't have to match; this makes the lint more conservative |
| 32 | + if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id); |
| 33 | + if cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b); |
| 34 | + if pat_contains_local(lhs.pat, a_id); |
| 35 | + if pat_contains_local(rhs.pat, b_id); |
| 36 | + then { |
| 37 | + entry.insert(b_id); |
| 38 | + true |
| 39 | + } else { |
| 40 | + false |
43 | 41 | }
|
44 |
| - }; |
45 |
| - // Arms with a guard are ignored, those can’t always be merged together |
46 |
| - // This is also the case for arms in-between each there is an arm with a guard |
47 |
| - (min_index..=max_index).all(|index| arms[index].guard.is_none()) |
48 |
| - && SpanlessEq::new(cx) |
49 |
| - .expr_fallback(eq_fallback) |
50 |
| - .eq_expr(lhs.body, rhs.body) |
51 |
| - // these checks could be removed to allow unused bindings |
52 |
| - && bindings_eq(lhs.pat, local_map.keys().copied().collect()) |
53 |
| - && bindings_eq(rhs.pat, local_map.values().copied().collect()) |
| 42 | + } |
54 | 43 | };
|
| 44 | + // Arms with a guard are ignored, those can’t always be merged together |
| 45 | + // This is also the case for arms in-between each there is an arm with a guard |
| 46 | + (min_index..=max_index).all(|index| arms[index].guard.is_none()) |
| 47 | + && SpanlessEq::new(cx) |
| 48 | + .expr_fallback(eq_fallback) |
| 49 | + .eq_expr(lhs.body, rhs.body) |
| 50 | + // these checks could be removed to allow unused bindings |
| 51 | + && bindings_eq(lhs.pat, local_map.keys().copied().collect()) |
| 52 | + && bindings_eq(rhs.pat, local_map.values().copied().collect()) |
| 53 | + }; |
55 | 54 |
|
56 |
| - let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect(); |
57 |
| - for (&(_, i), &(_, j)) in search_same(&indexed_arms, hash, eq) { |
58 |
| - span_lint_and_then( |
59 |
| - cx, |
60 |
| - MATCH_SAME_ARMS, |
61 |
| - j.body.span, |
62 |
| - "this `match` has identical arm bodies", |
63 |
| - |diag| { |
64 |
| - diag.span_note(i.body.span, "same as this"); |
| 55 | + let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect(); |
| 56 | + for (&(_, i), &(_, j)) in search_same(&indexed_arms, hash, eq) { |
| 57 | + span_lint_and_then( |
| 58 | + cx, |
| 59 | + MATCH_SAME_ARMS, |
| 60 | + j.body.span, |
| 61 | + "this `match` has identical arm bodies", |
| 62 | + |diag| { |
| 63 | + diag.span_note(i.body.span, "same as this"); |
65 | 64 |
|
66 |
| - // Note: this does not use `span_suggestion` on purpose: |
67 |
| - // there is no clean way |
68 |
| - // to remove the other arm. Building a span and suggest to replace it to "" |
69 |
| - // makes an even more confusing error message. Also in order not to make up a |
70 |
| - // span for the whole pattern, the suggestion is only shown when there is only |
71 |
| - // one pattern. The user should know about `|` if they are already using it… |
| 65 | + // Note: this does not use `span_suggestion` on purpose: |
| 66 | + // there is no clean way |
| 67 | + // to remove the other arm. Building a span and suggest to replace it to "" |
| 68 | + // makes an even more confusing error message. Also in order not to make up a |
| 69 | + // span for the whole pattern, the suggestion is only shown when there is only |
| 70 | + // one pattern. The user should know about `|` if they are already using it… |
72 | 71 |
|
73 |
| - let lhs = snippet(cx, i.pat.span, "<pat1>"); |
74 |
| - let rhs = snippet(cx, j.pat.span, "<pat2>"); |
| 72 | + let lhs = snippet(cx, i.pat.span, "<pat1>"); |
| 73 | + let rhs = snippet(cx, j.pat.span, "<pat2>"); |
75 | 74 |
|
76 |
| - if let PatKind::Wild = j.pat.kind { |
77 |
| - // if the last arm is _, then i could be integrated into _ |
78 |
| - // note that i.pat cannot be _, because that would mean that we're |
79 |
| - // hiding all the subsequent arms, and rust won't compile |
80 |
| - diag.span_note( |
81 |
| - i.body.span, |
82 |
| - &format!( |
83 |
| - "`{}` has the same arm body as the `_` wildcard, consider removing it", |
84 |
| - lhs |
85 |
| - ), |
86 |
| - ); |
87 |
| - } else { |
88 |
| - diag.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs,)) |
89 |
| - .help("...or consider changing the match arm bodies"); |
90 |
| - } |
91 |
| - }, |
92 |
| - ); |
93 |
| - } |
| 75 | + if let PatKind::Wild = j.pat.kind { |
| 76 | + // if the last arm is _, then i could be integrated into _ |
| 77 | + // note that i.pat cannot be _, because that would mean that we're |
| 78 | + // hiding all the subsequent arms, and rust won't compile |
| 79 | + diag.span_note( |
| 80 | + i.body.span, |
| 81 | + &format!( |
| 82 | + "`{}` has the same arm body as the `_` wildcard, consider removing it", |
| 83 | + lhs |
| 84 | + ), |
| 85 | + ); |
| 86 | + } else { |
| 87 | + diag.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs,)) |
| 88 | + .help("...or consider changing the match arm bodies"); |
| 89 | + } |
| 90 | + }, |
| 91 | + ); |
94 | 92 | }
|
95 | 93 | }
|
96 | 94 |
|
|
0 commit comments