Skip to content

Commit 13cb2b5

Browse files
committed
Move needless_collect into the methods lint pass
1 parent 6aa3684 commit 13cb2b5

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
121121
LintId::of(loops::MANUAL_MEMCPY),
122122
LintId::of(loops::MISSING_SPIN_LOOP),
123123
LintId::of(loops::MUT_RANGE_BOUND),
124-
LintId::of(loops::NEEDLESS_COLLECT),
125124
LintId::of(loops::NEEDLESS_RANGE_LOOP),
126125
LintId::of(loops::NEVER_LOOP),
127126
LintId::of(loops::SAME_ITEM_PUSH),
@@ -185,6 +184,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
185184
LintId::of(methods::MAP_COLLECT_RESULT_UNIT),
186185
LintId::of(methods::MAP_FLATTEN),
187186
LintId::of(methods::MAP_IDENTITY),
187+
LintId::of(methods::NEEDLESS_COLLECT),
188188
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
189189
LintId::of(methods::NEEDLESS_OPTION_TAKE),
190190
LintId::of(methods::NEEDLESS_SPLITN),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ store.register_lints(&[
236236
loops::MANUAL_MEMCPY,
237237
loops::MISSING_SPIN_LOOP,
238238
loops::MUT_RANGE_BOUND,
239-
loops::NEEDLESS_COLLECT,
240239
loops::NEEDLESS_RANGE_LOOP,
241240
loops::NEVER_LOOP,
242241
loops::SAME_ITEM_PUSH,
@@ -325,6 +324,7 @@ store.register_lints(&[
325324
methods::MAP_FLATTEN,
326325
methods::MAP_IDENTITY,
327326
methods::MAP_UNWRAP_OR,
327+
methods::NEEDLESS_COLLECT,
328328
methods::NEEDLESS_OPTION_AS_DEREF,
329329
methods::NEEDLESS_OPTION_TAKE,
330330
methods::NEEDLESS_SPLITN,

clippy_lints/src/lib.register_perf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![
1212
LintId::of(large_enum_variant::LARGE_ENUM_VARIANT),
1313
LintId::of(loops::MANUAL_MEMCPY),
1414
LintId::of(loops::MISSING_SPIN_LOOP),
15-
LintId::of(loops::NEEDLESS_COLLECT),
1615
LintId::of(methods::EXPECT_FUN_CALL),
1716
LintId::of(methods::EXTEND_WITH_DRAIN),
1817
LintId::of(methods::ITER_NTH),
1918
LintId::of(methods::ITER_OVEREAGER_CLONED),
2019
LintId::of(methods::MANUAL_STR_REPEAT),
20+
LintId::of(methods::NEEDLESS_COLLECT),
2121
LintId::of(methods::OR_FUN_CALL),
2222
LintId::of(methods::SINGLE_CHAR_PATTERN),
2323
LintId::of(methods::UNNECESSARY_TO_OWNED),

clippy_lints/src/loops/mod.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod manual_flatten;
99
mod manual_memcpy;
1010
mod missing_spin_loop;
1111
mod mut_range_bound;
12-
mod needless_collect;
1312
mod needless_range_loop;
1413
mod never_loop;
1514
mod same_item_push;
@@ -246,28 +245,6 @@ declare_clippy_lint! {
246245
"`loop { if let { ... } else break }`, which can be written as a `while let` loop"
247246
}
248247

249-
declare_clippy_lint! {
250-
/// ### What it does
251-
/// Checks for functions collecting an iterator when collect
252-
/// is not needed.
253-
///
254-
/// ### Why is this bad?
255-
/// `collect` causes the allocation of a new data structure,
256-
/// when this allocation may not be needed.
257-
///
258-
/// ### Example
259-
/// ```rust
260-
/// # let iterator = vec![1].into_iter();
261-
/// let len = iterator.clone().collect::<Vec<_>>().len();
262-
/// // should be
263-
/// let len = iterator.count();
264-
/// ```
265-
#[clippy::version = "1.30.0"]
266-
pub NEEDLESS_COLLECT,
267-
perf,
268-
"collecting an iterator when collect is not needed"
269-
}
270-
271248
declare_clippy_lint! {
272249
/// ### What it does
273250
/// Checks `for` loops over slices with an explicit counter
@@ -606,7 +583,6 @@ declare_lint_pass!(Loops => [
606583
ITER_NEXT_LOOP,
607584
FOR_LOOPS_OVER_FALLIBLES,
608585
WHILE_LET_LOOP,
609-
NEEDLESS_COLLECT,
610586
EXPLICIT_COUNTER_LOOP,
611587
EMPTY_LOOP,
612588
WHILE_LET_ON_ITERATOR,
@@ -668,8 +644,6 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
668644
while_immutable_condition::check(cx, condition, body);
669645
missing_spin_loop::check(cx, condition, body);
670646
}
671-
672-
needless_collect::check(expr, cx);
673647
}
674648
}
675649

clippy_lints/src/methods/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod map_collect_result_unit;
4242
mod map_flatten;
4343
mod map_identity;
4444
mod map_unwrap_or;
45+
mod needless_collect;
4546
mod needless_option_as_deref;
4647
mod needless_option_take;
4748
mod ok_expect;
@@ -2197,6 +2198,28 @@ declare_clippy_lint! {
21972198
"using `.as_ref().take()` on a temporary value"
21982199
}
21992200

2201+
declare_clippy_lint! {
2202+
/// ### What it does
2203+
/// Checks for functions collecting an iterator when collect
2204+
/// is not needed.
2205+
///
2206+
/// ### Why is this bad?
2207+
/// `collect` causes the allocation of a new data structure,
2208+
/// when this allocation may not be needed.
2209+
///
2210+
/// ### Example
2211+
/// ```rust
2212+
/// # let iterator = vec![1].into_iter();
2213+
/// let len = iterator.clone().collect::<Vec<_>>().len();
2214+
/// // should be
2215+
/// let len = iterator.count();
2216+
/// ```
2217+
#[clippy::version = "1.30.0"]
2218+
pub NEEDLESS_COLLECT,
2219+
perf,
2220+
"collecting an iterator when collect is not needed"
2221+
}
2222+
22002223
pub struct Methods {
22012224
avoid_breaking_exported_api: bool,
22022225
msrv: Option<RustcVersion>,
@@ -2287,6 +2310,7 @@ impl_lint_pass!(Methods => [
22872310
NEEDLESS_OPTION_AS_DEREF,
22882311
IS_DIGIT_ASCII_RADIX,
22892312
NEEDLESS_OPTION_TAKE,
2313+
NEEDLESS_COLLECT,
22902314
]);
22912315

22922316
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2335,6 +2359,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
23352359
},
23362360
_ => (),
23372361
}
2362+
2363+
needless_collect::check(expr, cx);
23382364
}
23392365

23402366
#[allow(clippy::too_many_lines)]

0 commit comments

Comments
 (0)