Simplify manual_unwrap_or

This commit is contained in:
Cameron Steffen 2021-05-16 17:40:29 -05:00
parent 1aad7541db
commit b2270e1f45

View File

@ -54,21 +54,6 @@ impl LateLintPass<'_> for ManualUnwrapOr {
}
}
#[derive(Copy, Clone)]
enum Case {
Option,
Result,
}
impl Case {
fn unwrap_fn_path(&self) -> &str {
match self {
Case::Option => "Option::unwrap_or",
Case::Result => "Result::unwrap_or",
}
}
}
fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
if_chain! {
@ -101,10 +86,10 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if_chain! {
if let ExprKind::Match(scrutinee, match_arms, _) = expr.kind;
let ty = cx.typeck_results().expr_ty(scrutinee);
if let Some(case) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
Some(Case::Option)
if let Some(ty_name) = if is_type_diagnostic_item(cx, ty, sym::option_type) {
Some("Option")
} else if is_type_diagnostic_item(cx, ty, sym::result_type) {
Some(Case::Result)
Some("Result")
} else {
None
};
@ -127,7 +112,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
span_lint_and_sugg(
cx,
MANUAL_UNWRAP_OR, expr.span,
&format!("this pattern reimplements `{}`", case.unwrap_fn_path()),
&format!("this pattern reimplements `{}::unwrap_or`", ty_name),
"replace with",
format!(
"{}.unwrap_or({})",