Skip to content

Commit 083a454

Browse files
committed
Small refactor on question_mark condition checks
1 parent 3fc99b6 commit 083a454

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ impl QuestionMark {
6060
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
6161
if let ExprKind::MethodCall(segment, _, args, _) = &cond.kind;
6262
if let Some(subject) = args.get(0);
63-
if (Self::is_option(cx, subject)
64-
&& Self::expression_returns_none(cx, then)
65-
&& segment.ident.name == sym!(is_none))
66-
||
67-
(Self::is_result(cx, subject)
68-
&& Self::expression_returns_unmodified_err(cx, then, subject)
69-
&& segment.ident.name == sym!(is_err));
63+
if (Self::option_check_and_early_return(cx, subject, then) && segment.ident.name == sym!(is_none)) ||
64+
(Self::result_check_and_early_return(cx, subject, then) && segment.ident.name == sym!(is_err));
7065
then {
7166
let mut applicability = Applicability::MachineApplicable;
7267
let receiver_str = &Sugg::hir_with_applicability(cx, subject, "..", &mut applicability);
@@ -109,13 +104,8 @@ impl QuestionMark {
109104
if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) })
110105
= higher::IfLet::hir(cx, expr);
111106
if let PatKind::TupleStruct(ref path1, fields, None) = let_pat.kind;
112-
if (Self::is_option(cx, let_expr)
113-
&& Self::expression_returns_none(cx, if_else)
114-
&& is_lang_ctor(cx, path1, OptionSome))
115-
||
116-
(Self::is_result(cx, let_expr)
117-
&& Self::expression_returns_unmodified_err(cx, if_else, let_expr)
118-
&& is_lang_ctor(cx, path1, ResultOk));
107+
if (Self::option_check_and_early_return(cx, let_expr, if_else) && is_lang_ctor(cx, path1, OptionSome)) ||
108+
(Self::result_check_and_early_return(cx, let_expr, if_else) && is_lang_ctor(cx, path1, ResultOk));
119109

120110
if let PatKind::Binding(annot, bind_id, _, _) = fields[0].kind;
121111
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
@@ -141,6 +131,14 @@ impl QuestionMark {
141131
}
142132
}
143133

134+
fn result_check_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>, nested_expr: &Expr<'_>) -> bool {
135+
Self::is_result(cx, expr) && Self::expression_returns_unmodified_err(cx, nested_expr, expr)
136+
}
137+
138+
fn option_check_and_early_return(cx: &LateContext<'_>, expr: &Expr<'_>, nested_expr: &Expr<'_>) -> bool {
139+
Self::is_option(cx, expr) && Self::expression_returns_none(cx, nested_expr)
140+
}
141+
144142
fn moves_by_default(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
145143
let expr_ty = cx.typeck_results().expr_ty(expression);
146144

0 commit comments

Comments
 (0)