File tree 3 files changed +43
-6
lines changed 3 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,13 @@ declare_clippy_lint! {
18
18
/// ### What it does
19
19
/// Checks for calls to `push` immediately after creating a new `Vec`.
20
20
///
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
+ ///
21
28
/// ### Why is this bad?
22
29
/// The `vec![]` macro is both more performant and easier to read than
23
30
/// multiple `push` calls.
@@ -56,7 +63,7 @@ struct VecPushSearcher {
56
63
}
57
64
impl VecPushSearcher {
58
65
fn display_err ( & self , cx : & LateContext < ' _ > ) {
59
- let min_pushes_for_extension = match self . init {
66
+ let required_pushes_before_extension = match self . init {
60
67
_ if self . found == 0 => return ,
61
68
VecInitKind :: WithConstCapacity ( x) if x > self . found => return ,
62
69
VecInitKind :: WithConstCapacity ( x) => x,
@@ -110,7 +117,7 @@ impl VecPushSearcher {
110
117
} ) ;
111
118
112
119
// 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 {
114
121
return ;
115
122
}
116
123
Original file line number Diff line number Diff line change @@ -29,6 +29,12 @@ fn main() {
29
29
// no lint
30
30
vec. push ( 1 ) ;
31
31
}
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 ) ;
32
38
}
33
39
34
40
pub fn no_lint ( ) -> Vec < i32 > {
@@ -84,5 +90,17 @@ fn _cond_push_with_large_start(x: bool) -> Vec<u32> {
84
90
if x {
85
91
v. push ( 1 ) ;
86
92
}
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
88
106
}
Original file line number Diff line number Diff line change @@ -31,15 +31,15 @@ LL | | new_err.push(0);
31
31
| |____________________^ help: consider using the `vec![]` macro: `new_err = vec![..];`
32
32
33
33
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
35
35
|
36
36
LL | / let mut v = Vec::new();
37
37
LL | | v.push(x);
38
38
LL | | v.push(1);
39
39
| |______________^ help: consider using the `vec![]` macro: `let mut v = vec![..];`
40
40
41
41
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
43
43
|
44
44
LL | / let mut v = Vec::new();
45
45
LL | | v.push(0);
@@ -50,5 +50,17 @@ LL | | v.push(1);
50
50
LL | | v.push(0);
51
51
| |______________^ help: consider using the `vec![]` macro: `let mut v = vec![..];`
52
52
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
54
66
You can’t perform that action at this time.
0 commit comments