Refactored the candidate type checking

This commit is contained in:
Micha White 2022-06-09 17:44:28 -04:00
parent 6d6c63ee23
commit 2967127de2
No known key found for this signature in database
GPG Key ID: AED94BFA1C301389

View File

@ -151,26 +151,17 @@ fn check_opt_like<'a>(
return; return;
} }
if paths_and_types.iter().all(|info| in_candidate_enum(cx, info)) { if paths_and_types.iter().all(|ty| in_candidate_enum(cx, *ty)) {
report_single_pattern(cx, ex, arms, expr, els); report_single_pattern(cx, ex, arms, expr, els);
} }
} }
fn in_candidate_enum<'a>(cx: &LateContext<'a>, path_info: &(String, Ty<'_>)) -> bool { fn in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'_>) -> bool {
// list of candidate `Enum`s we know will never get any more members // list of candidate `Enum`s we know will never get any more members
let candidates = &[ let candidates = [&paths::COW, &paths::OPTION, &paths::RESULT];
(&paths::COW, "Borrowed"),
(&paths::COW, "Cow::Borrowed"),
(&paths::COW, "Cow::Owned"),
(&paths::COW, "Owned"),
(&paths::OPTION, "None"),
(&paths::RESULT, "Err"),
(&paths::RESULT, "Ok"),
];
let (path, ty) = path_info; for candidate_ty in candidates {
for &(ty_path, pat_path) in candidates { if match_type(cx, ty, candidate_ty) {
if path == pat_path && match_type(cx, *ty, ty_path) {
return true; return true;
} }
} }
@ -179,29 +170,15 @@ fn in_candidate_enum<'a>(cx: &LateContext<'a>, path_info: &(String, Ty<'_>)) ->
/// Collects paths and their types from the given patterns. Returns true if the given pattern could /// Collects paths and their types from the given patterns. Returns true if the given pattern could
/// be simplified, false otherwise. /// be simplified, false otherwise.
fn collect_pat_paths<'a>(acc: &mut Vec<(String, Ty<'a>)>, cx: &LateContext<'a>, pat: &Pat<'_>, ty: Ty<'a>) -> bool { fn collect_pat_paths<'a>(acc: &mut Vec<Ty<'a>>, cx: &LateContext<'a>, pat: &Pat<'_>, ty: Ty<'a>) -> bool {
match pat.kind { match pat.kind {
PatKind::Wild => true, PatKind::Wild => true,
PatKind::Tuple(inner, _) => inner.iter().all(|p| { PatKind::Tuple(inner, _) => inner.iter().all(|p| {
let p_ty = cx.typeck_results().pat_ty(p); let p_ty = cx.typeck_results().pat_ty(p);
collect_pat_paths(acc, cx, p, p_ty) collect_pat_paths(acc, cx, p, p_ty)
}), }),
PatKind::TupleStruct(ref path, ..) => { PatKind::TupleStruct(..) | PatKind::Binding(BindingAnnotation::Unannotated, .., None) | PatKind::Path(_) => {
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| { acc.push(ty);
s.print_qpath(path, false);
});
acc.push((path, ty));
true
},
PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => {
acc.push((ident.to_string(), ty));
true
},
PatKind::Path(ref path) => {
let path = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| {
s.print_qpath(path, false);
});
acc.push((path, ty));
true true
}, },
_ => false, _ => false,
@ -265,14 +242,7 @@ fn form_exhaustive_matches<'a>(cx: &LateContext<'a>, ty: Ty<'a>, left: &Pat<'_>,
} }
true true
}, },
(PatKind::TupleStruct(..), PatKind::Path(_) | PatKind::TupleStruct(..)) => { (PatKind::TupleStruct(..), PatKind::Path(_) | PatKind::TupleStruct(..)) => in_candidate_enum(cx, ty),
let mut paths_and_types = Vec::new();
if !collect_pat_paths(&mut paths_and_types, cx, right, ty) {
return false;
}
paths_and_types.iter().all(|info| in_candidate_enum(cx, info))
},
_ => false, _ => false,
} }
} }