Auto merge of #11597 - y21:repeat_vec_with_capacity, r=dswij
new lint: `repeat_vec_with_capacity` Closes #11537 [Lint description](https://github.com/y21/rust-clippy/blob/repeat_vec_with_capacity/clippy_lints/src/repeat_vec_with_capacity.rs#L14) should explain this PR :) changelog: new lint: `repeat_vec_with_capacity`
This commit is contained in:
commit
5ac76ac54f
7 changed files with 234 additions and 0 deletions
38
tests/ui/repeat_vec_with_capacity.fixed
Normal file
38
tests/ui/repeat_vec_with_capacity.fixed
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#![warn(clippy::repeat_vec_with_capacity)]
|
||||
|
||||
fn main() {
|
||||
{
|
||||
(0..123).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>();
|
||||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
|
||||
}
|
||||
|
||||
{
|
||||
let n = 123;
|
||||
(0..n).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>();
|
||||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
|
||||
}
|
||||
|
||||
{
|
||||
macro_rules! from_macro {
|
||||
($x:expr) => {
|
||||
vec![$x; 123];
|
||||
};
|
||||
}
|
||||
// vec expansion is from another macro, don't lint
|
||||
from_macro!(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
|
||||
}
|
||||
|
||||
{
|
||||
macro_rules! from_macro {
|
||||
($x:expr) => {
|
||||
std::iter::repeat($x)
|
||||
};
|
||||
}
|
||||
from_macro!(Vec::<()>::with_capacity(42));
|
||||
}
|
||||
}
|
||||
38
tests/ui/repeat_vec_with_capacity.rs
Normal file
38
tests/ui/repeat_vec_with_capacity.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#![warn(clippy::repeat_vec_with_capacity)]
|
||||
|
||||
fn main() {
|
||||
{
|
||||
vec![Vec::<()>::with_capacity(42); 123];
|
||||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
|
||||
}
|
||||
|
||||
{
|
||||
let n = 123;
|
||||
vec![Vec::<()>::with_capacity(42); n];
|
||||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
|
||||
}
|
||||
|
||||
{
|
||||
macro_rules! from_macro {
|
||||
($x:expr) => {
|
||||
vec![$x; 123];
|
||||
};
|
||||
}
|
||||
// vec expansion is from another macro, don't lint
|
||||
from_macro!(Vec::<()>::with_capacity(42));
|
||||
}
|
||||
|
||||
{
|
||||
std::iter::repeat(Vec::<()>::with_capacity(42));
|
||||
//~^ ERROR: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity
|
||||
}
|
||||
|
||||
{
|
||||
macro_rules! from_macro {
|
||||
($x:expr) => {
|
||||
std::iter::repeat($x)
|
||||
};
|
||||
}
|
||||
from_macro!(Vec::<()>::with_capacity(42));
|
||||
}
|
||||
}
|
||||
40
tests/ui/repeat_vec_with_capacity.stderr
Normal file
40
tests/ui/repeat_vec_with_capacity.stderr
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
error: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity
|
||||
--> $DIR/repeat_vec_with_capacity.rs:5: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
|
||||
--> $DIR/repeat_vec_with_capacity.rs:11: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
|
||||
--> $DIR/repeat_vec_with_capacity.rs:26: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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue