@@ -54,6 +54,7 @@ mod map_flatten;
5454mod map_identity;
5555mod map_unwrap_or;
5656mod mut_mutex_lock;
57+ mod needless_collect;
5758mod needless_option_as_deref;
5859mod needless_option_take;
5960mod no_effect_replace;
@@ -3141,6 +3142,28 @@ declare_clippy_lint! {
31413142 "jumping to the start of stream using `seek` method"
31423143}
31433144
3145+ declare_clippy_lint ! {
3146+ /// ### What it does
3147+ /// Checks for functions collecting an iterator when collect
3148+ /// is not needed.
3149+ ///
3150+ /// ### Why is this bad?
3151+ /// `collect` causes the allocation of a new data structure,
3152+ /// when this allocation may not be needed.
3153+ ///
3154+ /// ### Example
3155+ /// ```rust
3156+ /// # let iterator = vec![1].into_iter();
3157+ /// let len = iterator.clone().collect::<Vec<_>>().len();
3158+ /// // should be
3159+ /// let len = iterator.count();
3160+ /// ```
3161+ #[ clippy:: version = "1.30.0" ]
3162+ pub NEEDLESS_COLLECT ,
3163+ nursery,
3164+ "collecting an iterator when collect is not needed"
3165+ }
3166+
31443167pub struct Methods {
31453168 avoid_breaking_exported_api : bool ,
31463169 msrv : Option < RustcVersion > ,
@@ -3267,6 +3290,7 @@ impl_lint_pass!(Methods => [
32673290 ITER_KV_MAP ,
32683291 SEEK_FROM_CURRENT ,
32693292 SEEK_TO_START_INSTEAD_OF_REWIND ,
3293+ NEEDLESS_COLLECT ,
32703294] ) ;
32713295
32723296/// Extracts a method call name, args, and `Span` of the method name.
@@ -3317,6 +3341,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
33173341 } ,
33183342 _ => ( ) ,
33193343 }
3344+
3345+ needless_collect:: check ( expr, cx) ;
33203346 }
33213347
33223348 #[ allow( clippy:: too_many_lines) ]
0 commit comments