@@ -52,75 +52,75 @@ struct Replacement {
52
52
has_generic_return : bool ,
53
53
}
54
54
55
- pub ( super ) fn check (
55
+ fn check_fold_with_op (
56
56
cx : & LateContext < ' _ > ,
57
57
expr : & hir:: Expr < ' _ > ,
58
- init : & hir:: Expr < ' _ > ,
59
58
acc : & hir:: Expr < ' _ > ,
60
59
fold_span : Span ,
60
+ op : hir:: BinOpKind ,
61
+ replacement : Replacement ,
61
62
) {
62
- fn check_fold_with_op (
63
- cx : & LateContext < ' _ > ,
64
- expr : & hir:: Expr < ' _ > ,
65
- acc : & hir:: Expr < ' _ > ,
66
- fold_span : Span ,
67
- op : hir:: BinOpKind ,
68
- replacement : Replacement ,
69
- ) {
70
- if_chain ! {
71
- // Extract the body of the closure passed to fold
72
- if let hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) = acc. kind;
73
- let closure_body = cx. tcx. hir( ) . body( body) ;
74
- let closure_expr = peel_blocks( closure_body. value) ;
75
-
76
- // Check if the closure body is of the form `acc <op> some_expr(x)`
77
- if let hir:: ExprKind :: Binary ( ref bin_op, left_expr, right_expr) = closure_expr. kind;
78
- if bin_op. node == op;
79
-
80
- // Extract the names of the two arguments to the closure
81
- if let [ param_a, param_b] = closure_body. params;
82
- if let PatKind :: Binding ( _, first_arg_id, ..) = strip_pat_refs( param_a. pat) . kind;
83
- if let PatKind :: Binding ( _, second_arg_id, second_arg_ident, _) = strip_pat_refs( param_b. pat) . kind;
84
-
85
- if path_to_local_id( left_expr, first_arg_id) ;
86
- if replacement. has_args || path_to_local_id( right_expr, second_arg_id) ;
87
-
88
- then {
89
- let mut applicability = Applicability :: MachineApplicable ;
90
-
91
- let turbofish = if replacement. has_generic_return {
92
- format!( "::<{}>" , cx. typeck_results( ) . expr_ty_adjusted( right_expr) . peel_refs( ) )
93
- } else {
94
- String :: new( )
95
- } ;
96
-
97
- let sugg = if replacement. has_args {
98
- format!(
99
- "{method}{turbofish}(|{second_arg_ident}| {r})" ,
100
- method = replacement. method_name,
101
- r = snippet_with_applicability( cx, right_expr. span, "EXPR" , & mut applicability) ,
102
- )
103
- } else {
104
- format!(
105
- "{method}{turbofish}()" ,
106
- method = replacement. method_name,
107
- )
108
- } ;
109
-
110
- span_lint_and_sugg(
111
- cx,
112
- UNNECESSARY_FOLD ,
113
- fold_span. with_hi( expr. span. hi( ) ) ,
114
- // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
115
- "this `.fold` can be written more succinctly using another method" ,
116
- "try" ,
117
- sugg,
118
- applicability,
119
- ) ;
120
- }
63
+ if_chain ! {
64
+ // Extract the body of the closure passed to fold
65
+ if let hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) = acc. kind;
66
+ let closure_body = cx. tcx. hir( ) . body( body) ;
67
+ let closure_expr = peel_blocks( closure_body. value) ;
68
+
69
+ // Check if the closure body is of the form `acc <op> some_expr(x)`
70
+ if let hir:: ExprKind :: Binary ( ref bin_op, left_expr, right_expr) = closure_expr. kind;
71
+ if bin_op. node == op;
72
+
73
+ // Extract the names of the two arguments to the closure
74
+ if let [ param_a, param_b] = closure_body. params;
75
+ if let PatKind :: Binding ( _, first_arg_id, ..) = strip_pat_refs( param_a. pat) . kind;
76
+ if let PatKind :: Binding ( _, second_arg_id, second_arg_ident, _) = strip_pat_refs( param_b. pat) . kind;
77
+
78
+ if path_to_local_id( left_expr, first_arg_id) ;
79
+ if replacement. has_args || path_to_local_id( right_expr, second_arg_id) ;
80
+
81
+ then {
82
+ let mut applicability = Applicability :: MachineApplicable ;
83
+
84
+ let turbofish = if replacement. has_generic_return {
85
+ format!( "::<{}>" , cx. typeck_results( ) . expr_ty_adjusted( right_expr) . peel_refs( ) )
86
+ } else {
87
+ String :: new( )
88
+ } ;
89
+
90
+ let sugg = if replacement. has_args {
91
+ format!(
92
+ "{method}{turbofish}(|{second_arg_ident}| {r})" ,
93
+ method = replacement. method_name,
94
+ r = snippet_with_applicability( cx, right_expr. span, "EXPR" , & mut applicability) ,
95
+ )
96
+ } else {
97
+ format!(
98
+ "{method}{turbofish}()" ,
99
+ method = replacement. method_name,
100
+ )
101
+ } ;
102
+
103
+ span_lint_and_sugg(
104
+ cx,
105
+ UNNECESSARY_FOLD ,
106
+ fold_span. with_hi( expr. span. hi( ) ) ,
107
+ // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
108
+ "this `.fold` can be written more succinctly using another method" ,
109
+ "try" ,
110
+ sugg,
111
+ applicability,
112
+ ) ;
121
113
}
122
114
}
115
+ }
123
116
117
+ pub ( super ) fn check (
118
+ cx : & LateContext < ' _ > ,
119
+ expr : & hir:: Expr < ' _ > ,
120
+ init : & hir:: Expr < ' _ > ,
121
+ acc : & hir:: Expr < ' _ > ,
122
+ fold_span : Span ,
123
+ ) {
124
124
// Check that this is a call to Iterator::fold rather than just some function called fold
125
125
if !is_trait_method ( cx, expr, sym:: Iterator ) {
126
126
return ;
0 commit comments