Use match on method args instead of if let

This commit is contained in:
sjwang05 2023-10-30 18:39:39 +00:00
parent 39eded7b05
commit 103200967e
No known key found for this signature in database
GPG Key ID: AB262FD6FFBFCFFE

View File

@ -65,27 +65,29 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
matches!(fm_method_str, "filter_map" | "flat_map" | "flatten") && matches!(fm_method_str, "filter_map" | "flat_map" | "flatten") &&
is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(fm_receiver), sym::IoLines) is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(fm_receiver), sym::IoLines)
{ {
let lint = if let [fm_arg] = fm_args { let lint = match fm_args {
match &fm_arg.kind { [] => fm_method_str == "flatten",
// Detect `Result::ok` [fm_arg] => {
ExprKind::Path(qpath) => match &fm_arg.kind {
cx.qpath_res(qpath, fm_arg.hir_id).opt_def_id().map(|did| // Detect `Result::ok`
match_def_path(cx, did, &paths::CORE_RESULT_OK_METHOD)).unwrap_or_default(), ExprKind::Path(qpath) =>
// Detect `|x| x.ok()` cx.qpath_res(qpath, fm_arg.hir_id).opt_def_id().map(|did|
ExprKind::Closure(Closure { body, .. }) => match_def_path(cx, did, &paths::CORE_RESULT_OK_METHOD)).unwrap_or_default(),
if let Body { params: [param], value, .. } = cx.tcx.hir().body(*body) && // Detect `|x| x.ok()`
let ExprKind::MethodCall(method, receiver, [], _) = value.kind && ExprKind::Closure(Closure { body, .. }) =>
path_to_local_id(receiver, param.pat.hir_id) && if let Body { params: [param], value, .. } = cx.tcx.hir().body(*body) &&
let Some(method_did) = cx.typeck_results().type_dependent_def_id(value.hir_id) let ExprKind::MethodCall(method, receiver, [], _) = value.kind &&
{ path_to_local_id(receiver, param.pat.hir_id) &&
is_diag_item_method(cx, method_did, sym::Result) && method.ident.as_str() == "ok" let Some(method_did) = cx.typeck_results().type_dependent_def_id(value.hir_id)
} else { {
false is_diag_item_method(cx, method_did, sym::Result) && method.ident.as_str() == "ok"
}, } else {
_ => false, false
},
_ => false,
}
} }
} else { _ => false,
fm_method_str == "flatten"
}; };
if lint { if lint {