Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2967127

Browse files
committed
Refactored the candidate type checking
1 parent 6d6c63e commit 2967127

File tree

1 file changed

+9
-39
lines changed

1 file changed

+9
-39
lines changed

clippy_lints/src/matches/single_match.rs

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,17 @@ fn check_opt_like<'a>(
151151
return;
152152
}
153153

154-
if paths_and_types.iter().all(|info| in_candidate_enum(cx, info)) {
154+
if paths_and_types.iter().all(|ty| in_candidate_enum(cx, *ty)) {
155155
report_single_pattern(cx, ex, arms, expr, els);
156156
}
157157
}
158158

159-
fn in_candidate_enum<'a>(cx: &LateContext<'a>, path_info: &(String, Ty<'_>)) -> bool {
159+
fn in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'_>) -> bool {
160160
// list of candidate `Enum`s we know will never get any more members
161-
let candidates = &[
162-
(&paths::COW, "Borrowed"),
163-
(&paths::COW, "Cow::Borrowed"),
164-
(&paths::COW, "Cow::Owned"),
165-
(&paths::COW, "Owned"),
166-
(&paths::OPTION, "None"),
167-
(&paths::RESULT, "Err"),
168-
(&paths::RESULT, "Ok"),
169-
];
161+
let candidates = [&paths::COW, &paths::OPTION, &paths::RESULT];
170162

171-
let (path, ty) = path_info;
172-
for &(ty_path, pat_path) in candidates {
173-
if path == pat_path && match_type(cx, *ty, ty_path) {
163+
for candidate_ty in candidates {
164+
if match_type(cx, ty, candidate_ty) {
174165
return true;
175166
}
176167
}
@@ -179,29 +170,15 @@ fn in_candidate_enum<'a>(cx: &LateContext<'a>, path_info: &(String, Ty<'_>)) ->
179170

180171
/// Collects paths and their types from the given patterns. Returns true if the given pattern could
181172
/// be simplified, false otherwise.
182-
fn collect_pat_paths<'a>(acc: &mut Vec<(String, Ty<'a>)>, cx: &LateContext<'a>, pat: &Pat<'_>, ty: Ty<'a>) -> bool {
173+
fn collect_pat_paths<'a>(acc: &mut Vec<Ty<'a>>, cx: &LateContext<'a>, pat: &Pat<'_>, ty: Ty<'a>) -> bool {
183174
match pat.kind {
184175
PatKind::Wild => true,
185176
PatKind::Tuple(inner, _) => inner.iter().all(|p| {
186177
let p_ty = cx.typeck_results().pat_ty(p);
187178
collect_pat_paths(acc, cx, p, p_ty)
188179
}),
189-
PatKind::TupleStruct(ref path, ..) => {
190-
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| {
191-
s.print_qpath(path, false);
192-
});
193-
acc.push((path, ty));
194-
true
195-
},
196-
PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => {
197-
acc.push((ident.to_string(), ty));
198-
true
199-
},
200-
PatKind::Path(ref path) => {
201-
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| {
202-
s.print_qpath(path, false);
203-
});
204-
acc.push((path, ty));
180+
PatKind::TupleStruct(..) | PatKind::Binding(BindingAnnotation::Unannotated, .., None) | PatKind::Path(_) => {
181+
acc.push(ty);
205182
true
206183
},
207184
_ => false,
@@ -265,14 +242,7 @@ fn form_exhaustive_matches<'a>(cx: &LateContext<'a>, ty: Ty<'a>, left: &Pat<'_>,
265242
}
266243
true
267244
},
268-
(PatKind::TupleStruct(..), PatKind::Path(_) | PatKind::TupleStruct(..)) => {
269-
let mut paths_and_types = Vec::new();
270-
if !collect_pat_paths(&mut paths_and_types, cx, right, ty) {
271-
return false;
272-
}
273-
274-
paths_and_types.iter().all(|info| in_candidate_enum(cx, info))
275-
},
245+
(PatKind::TupleStruct(..), PatKind::Path(_) | PatKind::TupleStruct(..)) => in_candidate_enum(cx, ty),
276246
_ => false,
277247
}
278248
}

0 commit comments

Comments
 (0)