@@ -7,7 +7,7 @@ use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths};
77use rustc_errors:: Applicability ;
88use rustc_hir:: { Expr , ExprKind } ;
99use rustc_lint:: { LateContext , LateLintPass } ;
10- use rustc_session:: { declare_lint_pass, declare_tool_lint } ;
10+ use rustc_session:: declare_lint_pass;
1111use rustc_span:: { sym, Span } ;
1212
1313declare_clippy_lint ! {
@@ -22,20 +22,25 @@ declare_clippy_lint! {
2222 /// expected that the yielded `Vec<_>` will have the requested capacity, otherwise one can simply write
2323 /// `iter::repeat(Vec::new())` instead and it will have the same effect.
2424 ///
25- /// Similarily for `vec![x; n]`, the element `x` is cloned to fill the vec.
25+ /// Similarly for `vec![x; n]`, the element `x` is cloned to fill the vec.
2626 /// Unlike `iter::repeat` however, the vec repeat macro does not have to clone the value `n` times
2727 /// but just `n - 1` times, because it can reuse the passed value for the last slot.
2828 /// That means that the last `Vec<_>` gets the requested capacity but all other ones do not.
2929 ///
3030 /// ### Example
3131 /// ```rust
32+ /// # use std::iter;
33+ ///
3234 /// let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
35+ /// let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
3336 /// ```
3437 /// Use instead:
3538 /// ```rust
36- /// let _: Vec<Vec<u8>> = (0..123).map(|_| Vec::with_capacity(42)).collect();
37- /// // ^^^ this closure executes 123 times
38- /// // and the vecs will have the expected capacity
39+ /// # use std::iter;
40+ ///
41+ /// let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
42+ /// // ^^^ this closure executes 123 times
43+ /// // and the vecs will have the expected capacity
3944 /// ```
4045 #[ clippy:: version = "1.74.0" ]
4146 pub REPEAT_VEC_WITH_CAPACITY ,
@@ -96,7 +101,7 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
96101 "iter::repeat" ,
97102 "none of the yielded `Vec`s will have the requested capacity" ,
98103 "if you intended to create an iterator that yields `Vec`s with an initial capacity, try" ,
99- format ! ( "std::iter::from_fn (|| Some({}) )" , snippet( cx, repeat_expr. span, ".." ) ) ,
104+ format ! ( "std::iter::repeat_with (|| {} )" , snippet( cx, repeat_expr. span, ".." ) ) ,
100105 ) ;
101106 }
102107}
0 commit comments