Skip to content

Commit 0e70f29

Browse files
committed
Update vec_init_then_push docs
1 parent 948af01 commit 0e70f29

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

clippy_lints/src/vec_init_then_push.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ declare_clippy_lint! {
1818
/// ### What it does
1919
/// Checks for calls to `push` immediately after creating a new `Vec`.
2020
///
21+
/// If the `Vec` is created using `with_capacity` this will only lint if the capacity is a
22+
/// constant and the number of pushes is greater than or equal to the initial capacity.
23+
///
24+
/// If the `Vec` is extended after the initial sequence of pushes and it was default initialized
25+
/// then this will only lint after there were at least four pushes. This number may change in
26+
/// the future.
27+
///
2128
/// ### Why is this bad?
2229
/// The `vec![]` macro is both more performant and easier to read than
2330
/// multiple `push` calls.
@@ -56,7 +63,7 @@ struct VecPushSearcher {
5663
}
5764
impl VecPushSearcher {
5865
fn display_err(&self, cx: &LateContext<'_>) {
59-
let min_pushes_for_extension = match self.init {
66+
let required_pushes_before_extension = match self.init {
6067
_ if self.found == 0 => return,
6168
VecInitKind::WithConstCapacity(x) if x > self.found => return,
6269
VecInitKind::WithConstCapacity(x) => x,
@@ -110,7 +117,7 @@ impl VecPushSearcher {
110117
});
111118

112119
// Avoid allocating small `Vec`s when they'll be extended right after.
113-
if res == ControlFlow::Break(true) && self.found <= min_pushes_for_extension {
120+
if res == ControlFlow::Break(true) && self.found <= required_pushes_before_extension {
114121
return;
115122
}
116123

tests/ui/vec_init_then_push.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ fn main() {
2929
// no lint
3030
vec.push(1);
3131
}
32+
33+
let mut vec = Vec::with_capacity(5);
34+
vec.push(1);
35+
vec.push(2);
36+
vec.push(3);
37+
vec.push(4);
3238
}
3339

3440
pub fn no_lint() -> Vec<i32> {
@@ -84,5 +90,17 @@ fn _cond_push_with_large_start(x: bool) -> Vec<u32> {
8490
if x {
8591
v.push(1);
8692
}
87-
v
93+
94+
let mut v2 = Vec::new();
95+
v2.push(0);
96+
v2.push(1);
97+
v2.push(0);
98+
v2.push(1);
99+
v2.push(0);
100+
v2.push(0);
101+
v2.push(1);
102+
v2.push(0);
103+
v2.extend(&v);
104+
105+
v2
88106
}

tests/ui/vec_init_then_push.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ LL | | new_err.push(0);
3131
| |____________________^ help: consider using the `vec![]` macro: `new_err = vec![..];`
3232

3333
error: calls to `push` immediately after creation
34-
--> $DIR/vec_init_then_push.rs:67:5
34+
--> $DIR/vec_init_then_push.rs:73:5
3535
|
3636
LL | / let mut v = Vec::new();
3737
LL | | v.push(x);
3838
LL | | v.push(1);
3939
| |______________^ help: consider using the `vec![]` macro: `let mut v = vec![..];`
4040

4141
error: calls to `push` immediately after creation
42-
--> $DIR/vec_init_then_push.rs:75:5
42+
--> $DIR/vec_init_then_push.rs:81:5
4343
|
4444
LL | / let mut v = Vec::new();
4545
LL | | v.push(0);
@@ -50,5 +50,17 @@ LL | | v.push(1);
5050
LL | | v.push(0);
5151
| |______________^ help: consider using the `vec![]` macro: `let mut v = vec![..];`
5252

53-
error: aborting due to 6 previous errors
53+
error: calls to `push` immediately after creation
54+
--> $DIR/vec_init_then_push.rs:94:5
55+
|
56+
LL | / let mut v2 = Vec::new();
57+
LL | | v2.push(0);
58+
LL | | v2.push(1);
59+
LL | | v2.push(0);
60+
... |
61+
LL | | v2.push(1);
62+
LL | | v2.push(0);
63+
| |_______________^ help: consider using the `vec![]` macro: `let mut v2 = vec![..];`
64+
65+
error: aborting due to 7 previous errors
5466

0 commit comments

Comments
 (0)