Skip to content

Commit 76eb781

Browse files
committed
use iter::repeat_with in suggestion and add examples
1 parent 5049415 commit 76eb781

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

clippy_lints/src/repeat_vec_with_capacity.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths};
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_session::declare_lint_pass;
1111
use rustc_span::{sym, Span};
1212

1313
declare_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
}

tests/ui/repeat_vec_with_capacity.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() {
2323
}
2424

2525
{
26-
std::iter::from_fn(|| Some(Vec::<()>::with_capacity(42)));
26+
std::iter::repeat_with(|| Vec::<()>::with_capacity(42));
2727
//~^ ERROR: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity
2828
}
2929

tests/ui/repeat_vec_with_capacity.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ LL | std::iter::repeat(Vec::<()>::with_capacity(42));
3333
= note: none of the yielded `Vec`s will have the requested capacity
3434
help: if you intended to create an iterator that yields `Vec`s with an initial capacity, try
3535
|
36-
LL | std::iter::from_fn(|| Some(Vec::<()>::with_capacity(42)));
37-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36+
LL | std::iter::repeat_with(|| Vec::<()>::with_capacity(42));
37+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3838

3939
error: aborting due to 3 previous errors
4040

0 commit comments

Comments
 (0)