@@ -60,13 +60,8 @@ impl QuestionMark {
60
60
if let Some ( higher:: If { cond, then, r#else } ) = higher:: If :: hir( expr) ;
61
61
if let ExprKind :: MethodCall ( segment, _, args, _) = & cond. kind;
62
62
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) ) ;
70
65
then {
71
66
let mut applicability = Applicability :: MachineApplicable ;
72
67
let receiver_str = & Sugg :: hir_with_applicability( cx, subject, ".." , & mut applicability) ;
@@ -109,13 +104,8 @@ impl QuestionMark {
109
104
if let Some ( higher:: IfLet { let_pat, let_expr, if_then, if_else: Some ( if_else) } )
110
105
= higher:: IfLet :: hir( cx, expr) ;
111
106
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 ) ) ;
119
109
120
110
if let PatKind :: Binding ( annot, bind_id, _, _) = fields[ 0 ] . kind;
121
111
let by_ref = matches!( annot, BindingAnnotation :: Ref | BindingAnnotation :: RefMut ) ;
@@ -141,6 +131,14 @@ impl QuestionMark {
141
131
}
142
132
}
143
133
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
+
144
142
fn moves_by_default ( cx : & LateContext < ' _ > , expression : & Expr < ' _ > ) -> bool {
145
143
let expr_ty = cx. typeck_results ( ) . expr_ty ( expression) ;
146
144
0 commit comments