rust/tests/ui/repeat_vec_with_capacity.stderr
Robert Spencer acc3842d43 Add new map_with_unused_argument_over_ranges lint
This lint checks for code that looks like
```rust
  let something : Vec<_> = (0..100).map(|_| {
    1 + 2 + 3
  }).collect();
```
which is more clear as
```rust
  let something : Vec<_> = std::iter::repeat_with(|| {
    1 + 2 + 3
  }).take(100).collect();
```
or
```rust
  let something : Vec<_> =
      std::iter::repeat_n(1 + 2 + 3, 100)
      .collect();
```

That is, a map over a range which does nothing with the parameter
passed to it is simply a function (or closure) being called `n`
times and could be more semantically expressed using `take`.
2024-10-29 21:32:00 +00:00

41 lines
1.8 KiB
Plaintext

error: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
--> tests/ui/repeat_vec_with_capacity.rs:6:9
|
LL | vec![Vec::<()>::with_capacity(42); 123];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: only the last `Vec` will have the capacity
= note: `-D clippy::repeat-vec-with-capacity` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::repeat_vec_with_capacity)]`
help: if you intended to initialize multiple `Vec`s with an initial capacity, try
|
LL | (0..123).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
--> tests/ui/repeat_vec_with_capacity.rs:12:9
|
LL | vec![Vec::<()>::with_capacity(42); n];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: only the last `Vec` will have the capacity
help: if you intended to initialize multiple `Vec`s with an initial capacity, try
|
LL | (0..n).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity
--> tests/ui/repeat_vec_with_capacity.rs:27:9
|
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::repeat_with(|| Vec::<()>::with_capacity(42));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors