clean-up unnecessary_unwrap a bit

replace multiple `if-let`s with `match`

turn the condition into a match guard

allows removing the `else` branch

replace `Vec::append` with `extend`

don't need the second vec after this operation anyway

remove `return`s
This commit is contained in:
Ada Alakbarova 2025-08-14 21:20:06 +02:00
parent 9563a5ce46
commit a8b52bacfe
No known key found for this signature in database

View file

@ -141,43 +141,45 @@ fn collect_unwrap_info<'tcx>(
is_type_diagnostic_item(cx, ty, sym::Result) && matches!(method_name, sym::is_err | sym::is_ok)
}
if let ExprKind::Binary(op, left, right) = &expr.kind {
match (invert, op.node) {
(false, BinOpKind::And | BinOpKind::BitAnd) | (true, BinOpKind::Or | BinOpKind::BitOr) => {
let mut unwrap_info = collect_unwrap_info(cx, if_expr, left, branch, invert, false);
unwrap_info.append(&mut collect_unwrap_info(cx, if_expr, right, branch, invert, false));
return unwrap_info;
},
_ => (),
}
} else if let ExprKind::Unary(UnOp::Not, expr) = &expr.kind {
return collect_unwrap_info(cx, if_expr, expr, branch, !invert, false);
} else if let ExprKind::MethodCall(method_name, receiver, [], _) = &expr.kind
&& let Some(local_id) = path_to_local(receiver)
&& let ty = cx.typeck_results().expr_ty(receiver)
&& let name = method_name.ident.name
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name))
{
let unwrappable = matches!(name, sym::is_some | sym::is_ok);
let safe_to_unwrap = unwrappable != invert;
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
UnwrappableKind::Option
} else {
UnwrappableKind::Result
};
match expr.kind {
ExprKind::Binary(op, left, right)
if matches!(
(invert, op.node),
(false, BinOpKind::And | BinOpKind::BitAnd) | (true, BinOpKind::Or | BinOpKind::BitOr)
) =>
{
let mut unwrap_info = collect_unwrap_info(cx, if_expr, left, branch, invert, false);
unwrap_info.extend(collect_unwrap_info(cx, if_expr, right, branch, invert, false));
unwrap_info
},
ExprKind::Unary(UnOp::Not, expr) => collect_unwrap_info(cx, if_expr, expr, branch, !invert, false),
ExprKind::MethodCall(method_name, receiver, [], _)
if let Some(local_id) = path_to_local(receiver)
&& let ty = cx.typeck_results().expr_ty(receiver)
&& let name = method_name.ident.name
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name)) =>
{
let unwrappable = matches!(name, sym::is_some | sym::is_ok);
let safe_to_unwrap = unwrappable != invert;
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
UnwrappableKind::Option
} else {
UnwrappableKind::Result
};
return vec![UnwrapInfo {
local_id,
if_expr,
check: expr,
check_name: name,
branch,
safe_to_unwrap,
kind,
is_entire_condition,
}];
vec![UnwrapInfo {
local_id,
if_expr,
check: expr,
check_name: name,
branch,
safe_to_unwrap,
kind,
is_entire_condition,
}]
},
_ => vec![],
}
Vec::new()
}
/// A HIR visitor delegate that checks if a local variable of type `Option` or `Result` is mutated,