@@ -12,14 +12,10 @@ use rustc_span::{source_map::Span, sym};
12
12
13
13
use super :: UNNECESSARY_FOLD ;
14
14
15
- /// No turbofish needed in any case.
16
- fn no_turbofish ( _: & LateContext < ' _ > , _: & hir:: Expr < ' _ > ) -> bool {
17
- false
18
- }
19
-
20
- /// Turbofish (`::<T>`) may be needed, but can be omitted if we are certain
21
- /// that the type can be inferred from usage.
22
- fn turbofish_if_not_inferred ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
15
+ /// Do we need to suggest turbofish when suggesting a replacement method?
16
+ /// Changing `fold` to `sum` needs it sometimes when the return type can't be
17
+ /// inferred. This checks for some common cases where it can be safely omitted
18
+ fn needs_turbofish ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
23
19
let parent = cx. tcx . hir ( ) . get_parent ( expr. hir_id ) ;
24
20
25
21
// some common cases where turbofish isn't needed:
@@ -53,26 +49,7 @@ fn turbofish_if_not_inferred(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool
53
49
struct Replacement {
54
50
method_name : & ' static str ,
55
51
has_args : bool ,
56
- requires_turbofish : fn ( & LateContext < ' _ > , & hir:: Expr < ' _ > ) -> bool ,
57
- }
58
- impl Replacement {
59
- /// `any(f)`, `all(f)`
60
- pub fn non_generic ( method_name : & ' static str ) -> Self {
61
- Self {
62
- method_name,
63
- has_args : true ,
64
- requires_turbofish : no_turbofish,
65
- }
66
- }
67
-
68
- /// `sum::<T>()`, `product::<T>()`
69
- pub fn generic ( method_name : & ' static str ) -> Self {
70
- Self {
71
- method_name,
72
- has_args : false ,
73
- requires_turbofish : turbofish_if_not_inferred,
74
- }
75
- }
52
+ has_generic_return : bool ,
76
53
}
77
54
78
55
pub ( super ) fn check (
@@ -111,7 +88,7 @@ pub(super) fn check(
111
88
then {
112
89
let mut applicability = Applicability :: MachineApplicable ;
113
90
114
- let turbofish = if ( replacement. requires_turbofish ) ( cx , expr ) {
91
+ let turbofish = if replacement. has_generic_return {
115
92
format!( "::<{}>" , cx. typeck_results( ) . expr_ty_adjusted( right_expr) . peel_refs( ) )
116
93
} else {
117
94
String :: new( )
@@ -159,7 +136,11 @@ pub(super) fn check(
159
136
acc,
160
137
fold_span,
161
138
hir:: BinOpKind :: Or ,
162
- Replacement :: non_generic ( "any" ) ,
139
+ Replacement {
140
+ has_args : true ,
141
+ has_generic_return : false ,
142
+ method_name : "any" ,
143
+ } ,
163
144
) ;
164
145
} ,
165
146
ast:: LitKind :: Bool ( true ) => {
@@ -169,7 +150,11 @@ pub(super) fn check(
169
150
acc,
170
151
fold_span,
171
152
hir:: BinOpKind :: And ,
172
- Replacement :: non_generic ( "all" ) ,
153
+ Replacement {
154
+ has_args : true ,
155
+ has_generic_return : false ,
156
+ method_name : "all" ,
157
+ } ,
173
158
) ;
174
159
} ,
175
160
ast:: LitKind :: Int ( 0 , _) => check_fold_with_op (
@@ -178,7 +163,11 @@ pub(super) fn check(
178
163
acc,
179
164
fold_span,
180
165
hir:: BinOpKind :: Add ,
181
- Replacement :: generic ( "sum" ) ,
166
+ Replacement {
167
+ has_args : false ,
168
+ has_generic_return : needs_turbofish ( cx, expr) ,
169
+ method_name : "sum" ,
170
+ } ,
182
171
) ,
183
172
ast:: LitKind :: Int ( 1 , _) => {
184
173
check_fold_with_op (
@@ -187,7 +176,11 @@ pub(super) fn check(
187
176
acc,
188
177
fold_span,
189
178
hir:: BinOpKind :: Mul ,
190
- Replacement :: generic ( "product" ) ,
179
+ Replacement {
180
+ has_args : false ,
181
+ has_generic_return : needs_turbofish ( cx, expr) ,
182
+ method_name : "product" ,
183
+ } ,
191
184
) ;
192
185
} ,
193
186
_ => ( ) ,
0 commit comments