@@ -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}
5764impl 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,
@@ -98,6 +105,8 @@ impl VecPushSearcher {
98105 && adjusted_mut == Mutability :: Mut
99106 && !adjusted_ty. peel_refs ( ) . is_slice ( ) =>
100107 {
108+ // No need to set `needs_mut` to true. The receiver will be either explicitly borrowed, or it will
109+ // be implicitly borrowed via an adjustment. Both of these cases are already handled by this point.
101110 return ControlFlow :: Break ( true ) ;
102111 } ,
103112 ExprKind :: Assign ( lhs, ..) if e. hir_id == lhs. hir_id => {
@@ -110,7 +119,7 @@ impl VecPushSearcher {
110119 } ) ;
111120
112121 // Avoid allocating small `Vec`s when they'll be extended right after.
113- if res == ControlFlow :: Break ( true ) && self . found <= min_pushes_for_extension {
122+ if res == ControlFlow :: Break ( true ) && self . found <= required_pushes_before_extension {
114123 return ;
115124 }
116125
0 commit comments