Skip to content

Commit aa8b09d

Browse files
authored
clean-up unnecessary_unwrap a bit (#15489)
changelog: none
2 parents 9563a5c + a8b52ba commit aa8b09d

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -141,43 +141,45 @@ fn collect_unwrap_info<'tcx>(
141141
is_type_diagnostic_item(cx, ty, sym::Result) && matches!(method_name, sym::is_err | sym::is_ok)
142142
}
143143

144-
if let ExprKind::Binary(op, left, right) = &expr.kind {
145-
match (invert, op.node) {
146-
(false, BinOpKind::And | BinOpKind::BitAnd) | (true, BinOpKind::Or | BinOpKind::BitOr) => {
147-
let mut unwrap_info = collect_unwrap_info(cx, if_expr, left, branch, invert, false);
148-
unwrap_info.append(&mut collect_unwrap_info(cx, if_expr, right, branch, invert, false));
149-
return unwrap_info;
150-
},
151-
_ => (),
152-
}
153-
} else if let ExprKind::Unary(UnOp::Not, expr) = &expr.kind {
154-
return collect_unwrap_info(cx, if_expr, expr, branch, !invert, false);
155-
} else if let ExprKind::MethodCall(method_name, receiver, [], _) = &expr.kind
156-
&& let Some(local_id) = path_to_local(receiver)
157-
&& let ty = cx.typeck_results().expr_ty(receiver)
158-
&& let name = method_name.ident.name
159-
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name))
160-
{
161-
let unwrappable = matches!(name, sym::is_some | sym::is_ok);
162-
let safe_to_unwrap = unwrappable != invert;
163-
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
164-
UnwrappableKind::Option
165-
} else {
166-
UnwrappableKind::Result
167-
};
144+
match expr.kind {
145+
ExprKind::Binary(op, left, right)
146+
if matches!(
147+
(invert, op.node),
148+
(false, BinOpKind::And | BinOpKind::BitAnd) | (true, BinOpKind::Or | BinOpKind::BitOr)
149+
) =>
150+
{
151+
let mut unwrap_info = collect_unwrap_info(cx, if_expr, left, branch, invert, false);
152+
unwrap_info.extend(collect_unwrap_info(cx, if_expr, right, branch, invert, false));
153+
unwrap_info
154+
},
155+
ExprKind::Unary(UnOp::Not, expr) => collect_unwrap_info(cx, if_expr, expr, branch, !invert, false),
156+
ExprKind::MethodCall(method_name, receiver, [], _)
157+
if let Some(local_id) = path_to_local(receiver)
158+
&& let ty = cx.typeck_results().expr_ty(receiver)
159+
&& let name = method_name.ident.name
160+
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name)) =>
161+
{
162+
let unwrappable = matches!(name, sym::is_some | sym::is_ok);
163+
let safe_to_unwrap = unwrappable != invert;
164+
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
165+
UnwrappableKind::Option
166+
} else {
167+
UnwrappableKind::Result
168+
};
168169

169-
return vec![UnwrapInfo {
170-
local_id,
171-
if_expr,
172-
check: expr,
173-
check_name: name,
174-
branch,
175-
safe_to_unwrap,
176-
kind,
177-
is_entire_condition,
178-
}];
170+
vec![UnwrapInfo {
171+
local_id,
172+
if_expr,
173+
check: expr,
174+
check_name: name,
175+
branch,
176+
safe_to_unwrap,
177+
kind,
178+
is_entire_condition,
179+
}]
180+
},
181+
_ => vec![],
179182
}
180-
Vec::new()
181183
}
182184

183185
/// A HIR visitor delegate that checks if a local variable of type `Option` or `Result` is mutated,

0 commit comments

Comments
 (0)