Skip to content

Commit 69e892e

Browse files
committed
get rid of unnecessary function pointer
1 parent c9daec2 commit 69e892e

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

clippy_lints/src/methods/unnecessary_fold.rs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ use rustc_span::{source_map::Span, sym};
1212

1313
use super::UNNECESSARY_FOLD;
1414

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 {
2319
let parent = cx.tcx.hir().get_parent(expr.hir_id);
2420

2521
// some common cases where turbofish isn't needed:
@@ -53,26 +49,7 @@ fn turbofish_if_not_inferred(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool
5349
struct Replacement {
5450
method_name: &'static str,
5551
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,
7653
}
7754

7855
pub(super) fn check(
@@ -111,7 +88,7 @@ pub(super) fn check(
11188
then {
11289
let mut applicability = Applicability::MachineApplicable;
11390

114-
let turbofish = if (replacement.requires_turbofish)(cx, expr) {
91+
let turbofish = if replacement.has_generic_return {
11592
format!("::<{}>", cx.typeck_results().expr_ty_adjusted(right_expr).peel_refs())
11693
} else {
11794
String::new()
@@ -159,7 +136,11 @@ pub(super) fn check(
159136
acc,
160137
fold_span,
161138
hir::BinOpKind::Or,
162-
Replacement::non_generic("any"),
139+
Replacement {
140+
has_args: true,
141+
has_generic_return: false,
142+
method_name: "any",
143+
},
163144
);
164145
},
165146
ast::LitKind::Bool(true) => {
@@ -169,7 +150,11 @@ pub(super) fn check(
169150
acc,
170151
fold_span,
171152
hir::BinOpKind::And,
172-
Replacement::non_generic("all"),
153+
Replacement {
154+
has_args: true,
155+
has_generic_return: false,
156+
method_name: "all",
157+
},
173158
);
174159
},
175160
ast::LitKind::Int(0, _) => check_fold_with_op(
@@ -178,7 +163,11 @@ pub(super) fn check(
178163
acc,
179164
fold_span,
180165
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+
},
182171
),
183172
ast::LitKind::Int(1, _) => {
184173
check_fold_with_op(
@@ -187,7 +176,11 @@ pub(super) fn check(
187176
acc,
188177
fold_span,
189178
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+
},
191184
);
192185
},
193186
_ => (),

0 commit comments

Comments
 (0)