diff --git a/clippy_lints/src/repeat_vec_with_capacity.rs b/clippy_lints/src/repeat_vec_with_capacity.rs index 62e049e54be5..5a4933a3fceb 100644 --- a/clippy_lints/src/repeat_vec_with_capacity.rs +++ b/clippy_lints/src/repeat_vec_with_capacity.rs @@ -7,7 +7,7 @@ use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::declare_lint_pass; use rustc_span::{sym, Span}; declare_clippy_lint! { @@ -22,20 +22,25 @@ declare_clippy_lint! { /// expected that the yielded `Vec<_>` will have the requested capacity, otherwise one can simply write /// `iter::repeat(Vec::new())` instead and it will have the same effect. /// - /// Similarily for `vec![x; n]`, the element `x` is cloned to fill the vec. + /// Similarly for `vec![x; n]`, the element `x` is cloned to fill the vec. /// Unlike `iter::repeat` however, the vec repeat macro does not have to clone the value `n` times /// but just `n - 1` times, because it can reuse the passed value for the last slot. /// That means that the last `Vec<_>` gets the requested capacity but all other ones do not. /// /// ### Example /// ```rust + /// # use std::iter; + /// /// let _: Vec> = vec![Vec::with_capacity(42); 123]; + /// let _: Vec> = iter::repeat(Vec::with_capacity(42)).take(123).collect(); /// ``` /// Use instead: /// ```rust - /// let _: Vec> = (0..123).map(|_| Vec::with_capacity(42)).collect(); - /// // ^^^ this closure executes 123 times - /// // and the vecs will have the expected capacity + /// # use std::iter; + /// + /// let _: Vec> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect(); + /// // ^^^ this closure executes 123 times + /// // and the vecs will have the expected capacity /// ``` #[clippy::version = "1.74.0"] pub REPEAT_VEC_WITH_CAPACITY, @@ -96,7 +101,7 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) { "iter::repeat", "none of the yielded `Vec`s will have the requested capacity", "if you intended to create an iterator that yields `Vec`s with an initial capacity, try", - format!("std::iter::from_fn(|| Some({}))", snippet(cx, repeat_expr.span, "..")), + format!("std::iter::repeat_with(|| {})", snippet(cx, repeat_expr.span, "..")), ); } } diff --git a/tests/ui/repeat_vec_with_capacity.fixed b/tests/ui/repeat_vec_with_capacity.fixed index 7a659b36467b..2afe2f433258 100644 --- a/tests/ui/repeat_vec_with_capacity.fixed +++ b/tests/ui/repeat_vec_with_capacity.fixed @@ -23,7 +23,7 @@ fn main() { } { - std::iter::from_fn(|| Some(Vec::<()>::with_capacity(42))); + std::iter::repeat_with(|| Vec::<()>::with_capacity(42)); //~^ ERROR: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity } diff --git a/tests/ui/repeat_vec_with_capacity.stderr b/tests/ui/repeat_vec_with_capacity.stderr index 7e77212bca10..10b5f121420e 100644 --- a/tests/ui/repeat_vec_with_capacity.stderr +++ b/tests/ui/repeat_vec_with_capacity.stderr @@ -33,8 +33,8 @@ LL | std::iter::repeat(Vec::<()>::with_capacity(42)); = note: none of the yielded `Vec`s will have the requested capacity help: if you intended to create an iterator that yields `Vec`s with an initial capacity, try | -LL | std::iter::from_fn(|| Some(Vec::<()>::with_capacity(42))); - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | std::iter::repeat_with(|| Vec::<()>::with_capacity(42)); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 3 previous errors