@@ -7,7 +7,7 @@ use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths};
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: { Expr , ExprKind } ;
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
- use rustc_session:: { declare_lint_pass, declare_tool_lint } ;
10
+ use rustc_session:: declare_lint_pass;
11
11
use rustc_span:: { sym, Span } ;
12
12
13
13
declare_clippy_lint ! {
@@ -22,20 +22,25 @@ declare_clippy_lint! {
22
22
/// expected that the yielded `Vec<_>` will have the requested capacity, otherwise one can simply write
23
23
/// `iter::repeat(Vec::new())` instead and it will have the same effect.
24
24
///
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.
26
26
/// Unlike `iter::repeat` however, the vec repeat macro does not have to clone the value `n` times
27
27
/// but just `n - 1` times, because it can reuse the passed value for the last slot.
28
28
/// That means that the last `Vec<_>` gets the requested capacity but all other ones do not.
29
29
///
30
30
/// ### Example
31
31
/// ```rust
32
+ /// # use std::iter;
33
+ ///
32
34
/// let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
35
+ /// let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
33
36
/// ```
34
37
/// Use instead:
35
38
/// ```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
39
44
/// ```
40
45
#[ clippy:: version = "1.74.0" ]
41
46
pub REPEAT_VEC_WITH_CAPACITY ,
@@ -96,7 +101,7 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
96
101
"iter::repeat" ,
97
102
"none of the yielded `Vec`s will have the requested capacity" ,
98
103
"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, ".." ) ) ,
100
105
) ;
101
106
}
102
107
}
0 commit comments