@@ -50,21 +50,16 @@ declare_lint_pass!(UseRetain => [USE_RETAIN]);
5050
5151impl < ' tcx > LateLintPass < ' tcx > for UseRetain {
5252 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > ) {
53- if_chain ! {
54- if let Some ( parent_expr) = get_parent_expr( cx, expr) ;
55- if let Assign ( left_expr, collect_expr, _) = & parent_expr. kind;
56- if let hir:: ExprKind :: MethodCall ( seg, _, _) = & collect_expr. kind;
57- if seg. args. is_none( ) ;
58-
59- if let hir:: ExprKind :: MethodCall ( _, [ target_expr] , _) = & collect_expr. kind;
60- if let Some ( collect_def_id) = cx. typeck_results( ) . type_dependent_def_id( collect_expr. hir_id) ;
61- if match_def_path( cx, collect_def_id, & paths:: CORE_ITER_COLLECT ) ;
62-
63- then {
64- check_into_iter( cx, parent_expr, left_expr, target_expr) ;
65- check_iter( cx, parent_expr, left_expr, target_expr) ;
66- check_to_owned( cx, parent_expr, left_expr, target_expr) ;
67- }
53+ if let Some ( parent_expr) = get_parent_expr ( cx, expr)
54+ && let Assign ( left_expr, collect_expr, _) = & parent_expr. kind
55+ && let hir:: ExprKind :: MethodCall ( seg, _, _) = & collect_expr. kind
56+ && seg. args . is_none ( )
57+ && let hir:: ExprKind :: MethodCall ( _, [ target_expr] , _) = & collect_expr. kind
58+ && let Some ( collect_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( collect_expr. hir_id )
59+ && match_def_path ( cx, collect_def_id, & paths:: CORE_ITER_COLLECT ) {
60+ check_into_iter ( cx, parent_expr, left_expr, target_expr) ;
61+ check_iter ( cx, parent_expr, left_expr, target_expr) ;
62+ check_to_owned ( cx, parent_expr, left_expr, target_expr) ;
6863 }
6964 }
7065}
@@ -75,21 +70,15 @@ fn check_into_iter(
7570 left_expr : & hir:: Expr < ' _ > ,
7671 target_expr : & hir:: Expr < ' _ > ,
7772) {
78- if_chain ! {
79- if let hir:: ExprKind :: MethodCall ( _, [ into_iter_expr, _] , _) = & target_expr. kind;
80- if let Some ( filter_def_id) = cx. typeck_results( ) . type_dependent_def_id( target_expr. hir_id) ;
81- if match_def_path( cx, filter_def_id, & paths:: CORE_ITER_FILTER ) ;
82-
83- if let hir:: ExprKind :: MethodCall ( _, [ struct_expr] , _) = & into_iter_expr. kind;
84- if let Some ( into_iter_def_id) = cx. typeck_results( ) . type_dependent_def_id( into_iter_expr. hir_id) ;
85- if match_def_path( cx, into_iter_def_id, & paths:: CORE_ITER_INTO_ITER ) ;
86- if match_acceptable_type( cx, left_expr) ;
87-
88- if SpanlessEq :: new( cx) . eq_expr( left_expr, struct_expr) ;
89-
90- then {
91- suggest( cx, parent_expr, left_expr, target_expr) ;
92- }
73+ if let hir:: ExprKind :: MethodCall ( _, [ into_iter_expr, _] , _) = & target_expr. kind
74+ && let Some ( filter_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( target_expr. hir_id )
75+ && match_def_path ( cx, filter_def_id, & paths:: CORE_ITER_FILTER )
76+ && let hir:: ExprKind :: MethodCall ( _, [ struct_expr] , _) = & into_iter_expr. kind
77+ && let Some ( into_iter_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( into_iter_expr. hir_id )
78+ && match_def_path ( cx, into_iter_def_id, & paths:: CORE_ITER_INTO_ITER )
79+ && match_acceptable_type ( cx, left_expr)
80+ && SpanlessEq :: new ( cx) . eq_expr ( left_expr, struct_expr) {
81+ suggest ( cx, parent_expr, left_expr, target_expr) ;
9382 }
9483}
9584
@@ -99,24 +88,19 @@ fn check_iter(
9988 left_expr : & hir:: Expr < ' _ > ,
10089 target_expr : & hir:: Expr < ' _ > ,
10190) {
102- if_chain ! {
103- if let hir:: ExprKind :: MethodCall ( _, [ filter_expr] , _) = & target_expr. kind;
104- if let Some ( copied_def_id) = cx. typeck_results( ) . type_dependent_def_id( target_expr. hir_id) ;
105- if match_def_path( cx, copied_def_id, & paths:: CORE_ITER_COPIED ) || match_def_path( cx, copied_def_id, & paths:: CORE_ITER_CLONED ) ;
106-
107- if let hir:: ExprKind :: MethodCall ( _, [ iter_expr, _] , _) = & filter_expr. kind;
108- if let Some ( filter_def_id) = cx. typeck_results( ) . type_dependent_def_id( filter_expr. hir_id) ;
109- if match_def_path( cx, filter_def_id, & paths:: CORE_ITER_FILTER ) ;
110-
111- if let hir:: ExprKind :: MethodCall ( _, [ struct_expr] , _) = & iter_expr. kind;
112- if let Some ( iter_expr_def_id) = cx. typeck_results( ) . type_dependent_def_id( iter_expr. hir_id) ;
113- if match_acceptable_def_path( cx, iter_expr_def_id) ;
114- if match_acceptable_type( cx, left_expr) ;
115- if SpanlessEq :: new( cx) . eq_expr( left_expr, struct_expr) ;
116-
117- then {
118- suggest( cx, parent_expr, left_expr, filter_expr) ;
119- }
91+ if let hir:: ExprKind :: MethodCall ( _, [ filter_expr] , _) = & target_expr. kind
92+ && let Some ( copied_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( target_expr. hir_id )
93+ && ( match_def_path ( cx, copied_def_id, & paths:: CORE_ITER_COPIED )
94+ || match_def_path ( cx, copied_def_id, & paths:: CORE_ITER_CLONED ) )
95+ && let hir:: ExprKind :: MethodCall ( _, [ iter_expr, _] , _) = & filter_expr. kind
96+ && let Some ( filter_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( filter_expr. hir_id )
97+ && match_def_path ( cx, filter_def_id, & paths:: CORE_ITER_FILTER )
98+ && let hir:: ExprKind :: MethodCall ( _, [ struct_expr] , _) = & iter_expr. kind
99+ && let Some ( iter_expr_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( iter_expr. hir_id )
100+ && match_acceptable_def_path ( cx, iter_expr_def_id)
101+ && match_acceptable_type ( cx, left_expr)
102+ && SpanlessEq :: new ( cx) . eq_expr ( left_expr, struct_expr) {
103+ suggest ( cx, parent_expr, left_expr, filter_expr) ;
120104 }
121105}
122106
@@ -126,36 +110,28 @@ fn check_to_owned(
126110 left_expr : & hir:: Expr < ' _ > ,
127111 target_expr : & hir:: Expr < ' _ > ,
128112) {
129- if_chain ! {
130- if let hir:: ExprKind :: MethodCall ( _, [ filter_expr] , _) = & target_expr. kind;
131- if let Some ( to_owned_def_id) = cx. typeck_results( ) . type_dependent_def_id( target_expr. hir_id) ;
132- if match_def_path( cx, to_owned_def_id, & paths:: TO_OWNED_METHOD ) ;
133-
134- if let hir:: ExprKind :: MethodCall ( _, [ chars_expr, _] , _) = & filter_expr. kind;
135- if let Some ( filter_def_id) = cx. typeck_results( ) . type_dependent_def_id( filter_expr. hir_id) ;
136- if match_def_path( cx, filter_def_id, & paths:: CORE_ITER_FILTER ) ;
137-
138- if let hir:: ExprKind :: MethodCall ( _, [ str_expr] , _) = & chars_expr. kind;
139- if let Some ( chars_expr_def_id) = cx. typeck_results( ) . type_dependent_def_id( chars_expr. hir_id) ;
140- if match_def_path( cx, chars_expr_def_id, & paths:: STR_CHARS ) ;
141-
142- let ty = cx. typeck_results( ) . expr_ty( str_expr) . peel_refs( ) ;
143- if is_type_diagnostic_item( cx, ty, sym:: String ) ;
144- if SpanlessEq :: new( cx) . eq_expr( left_expr, str_expr) ;
145-
146- then {
147- suggest( cx, parent_expr, left_expr, filter_expr) ;
148- }
113+ if let hir:: ExprKind :: MethodCall ( _, [ filter_expr] , _) = & target_expr. kind
114+ && let Some ( to_owned_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( target_expr. hir_id )
115+ && match_def_path ( cx, to_owned_def_id, & paths:: TO_OWNED_METHOD )
116+ && let hir:: ExprKind :: MethodCall ( _, [ chars_expr, _] , _) = & filter_expr. kind
117+ && let Some ( filter_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( filter_expr. hir_id )
118+ && match_def_path ( cx, filter_def_id, & paths:: CORE_ITER_FILTER )
119+ && let hir:: ExprKind :: MethodCall ( _, [ str_expr] , _) = & chars_expr. kind
120+ && let Some ( chars_expr_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( chars_expr. hir_id )
121+ && match_def_path ( cx, chars_expr_def_id, & paths:: STR_CHARS )
122+ && let ty = cx. typeck_results ( ) . expr_ty ( str_expr) . peel_refs ( )
123+ && is_type_diagnostic_item ( cx, ty, sym:: String )
124+ && SpanlessEq :: new ( cx) . eq_expr ( left_expr, str_expr) {
125+ suggest ( cx, parent_expr, left_expr, filter_expr) ;
149126 }
150127}
151128
152129fn suggest ( cx : & LateContext < ' _ > , parent_expr : & hir:: Expr < ' _ > , left_expr : & hir:: Expr < ' _ > , filter_expr : & hir:: Expr < ' _ > ) {
153- if_chain ! {
154- if let hir:: ExprKind :: MethodCall ( _, [ _, closure] , _) = filter_expr. kind;
155- if let hir:: ExprKind :: Closure ( _, _, filter_body_id, ..) = closure. kind;
156- let filter_body = cx. tcx. hir( ) . body( filter_body_id) ;
157- if let [ filter_params] = filter_body. params;
158- if let Some ( sugg) = match filter_params. pat. kind {
130+ if let hir:: ExprKind :: MethodCall ( _, [ _, closure] , _) = filter_expr. kind
131+ && let hir:: ExprKind :: Closure ( _, _, filter_body_id, ..) = closure. kind
132+ && let filter_body = cx. tcx . hir ( ) . body ( filter_body_id)
133+ && let [ filter_params] = filter_body. params
134+ && let Some ( sugg) = match filter_params. pat . kind {
159135 hir:: PatKind :: Binding ( _, _, filter_param_ident, None ) => {
160136 Some ( format ! ( "{}.retain(|{}| {})" , snippet( cx, left_expr. span, ".." ) , filter_param_ident, snippet( cx, filter_body. value. span, ".." ) ) )
161137 } ,
@@ -171,18 +147,16 @@ fn suggest(cx: &LateContext<'_>, parent_expr: &hir::Expr<'_>, left_expr: &hir::E
171147 }
172148 } ,
173149 _ => None
174- } ;
175- then {
176- span_lint_and_sugg(
177- cx,
178- USE_RETAIN ,
179- parent_expr. span,
180- "this expression can be written more simply using `.retain()`" ,
181- "consider calling `.retain()` instead" ,
182- sugg,
183- Applicability :: MachineApplicable
184- ) ;
185- }
150+ } {
151+ span_lint_and_sugg (
152+ cx,
153+ USE_RETAIN ,
154+ parent_expr. span ,
155+ "this expression can be written more simply using `.retain()`" ,
156+ "consider calling `.retain()` instead" ,
157+ sugg,
158+ Applicability :: MachineApplicable
159+ ) ;
186160 }
187161}
188162
0 commit comments