Skip to content

Commit 0a81abe

Browse files
committed
Refactoring pt. 2
1 parent 47eb021 commit 0a81abe

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,10 @@ fn should_wrap_in_braces(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
179179
})
180180
}
181181

182-
fn format_option_in_sugg(
183-
cx: &LateContext<'_, '_>,
184-
cond_expr: &Expr<'_>,
185-
parens_around_option: bool,
186-
as_ref: bool,
187-
as_mut: bool,
188-
) -> String {
182+
fn format_option_in_sugg(cx: &LateContext<'_, '_>, cond_expr: &Expr<'_>, as_ref: bool, as_mut: bool) -> String {
189183
format!(
190-
"{}{}{}{}",
191-
if parens_around_option { "(" } else { "" },
192-
Sugg::hir(cx, cond_expr, ".."),
193-
if parens_around_option { ")" } else { "" },
184+
"{}{}",
185+
Sugg::hir(cx, cond_expr, "..").maybe_par(),
194186
if as_mut {
195187
".as_mut()"
196188
} else if as_ref {
@@ -230,28 +222,13 @@ fn detect_option_if_let_else<'a>(cx: &LateContext<'_, 'a>, expr: &'a Expr<'a>) -
230222
ExprKind::AddrOf(_, Mutability::Mut, _) => (false, true),
231223
_ => (bind_annotation == &BindingAnnotation::Ref, bind_annotation == &BindingAnnotation::RefMut),
232224
};
233-
let parens_around_option = match &cond_expr.kind {
234-
// Put parens around the option expression if not doing so might
235-
// mess up the order of operations.
236-
ExprKind::Call(..)
237-
| ExprKind::MethodCall(..)
238-
| ExprKind::Loop(..)
239-
| ExprKind::Match(..)
240-
| ExprKind::Block(..)
241-
| ExprKind::Field(..)
242-
| ExprKind::Path(_)
243-
| ExprKind::Unary(UnOp::UnDeref, _)
244-
| ExprKind::AddrOf(..)
245-
=> false,
246-
_ => true,
247-
};
248225
let cond_expr = match &cond_expr.kind {
249226
// Pointer dereferencing happens automatically, so we can omit it in the suggestion
250-
ExprKind::Unary(UnOp::UnDeref, expr)|ExprKind::AddrOf(_, _, expr) => expr,
227+
ExprKind::Unary(UnOp::UnDeref, expr) | ExprKind::AddrOf(_, _, expr) => expr,
251228
_ => cond_expr,
252229
};
253230
Some(OptionIfLetElseOccurence {
254-
option: format_option_in_sugg(cx, cond_expr, parens_around_option, as_ref, as_mut),
231+
option: format_option_in_sugg(cx, cond_expr, as_ref, as_mut),
255232
method_sugg: method_sugg.to_string(),
256233
some_expr: format!("|{}{}| {}", capture_mut, capture_name, Sugg::hir(cx, some_body, "..")),
257234
none_expr: format!("{}{}", if method_sugg == "map_or" { "" } else { "|| " }, Sugg::hir(cx, none_body, "..")),

0 commit comments

Comments
 (0)