Skip to content

Commit

Permalink
Update vec_init_then_push docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Apr 27, 2022
1 parent 948af01 commit 94ec421
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions clippy_lints/src/vec_init_then_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ declare_clippy_lint! {
/// ### What it does
/// Checks for calls to `push` immediately after creating a new `Vec`.
///
/// If the `Vec` is created using `with_capacity` this will only lint if the capacity is a
/// constant and the number of pushes is greater than or equal to the initial capacity.
///
/// If the `Vec` is extended after the initial sequence of pushes and it was default initialized
/// then this will only lint after there were at least four pushes. This number may change in
/// the future.
///
/// ### Why is this bad?
/// The `vec![]` macro is both more performant and easier to read than
/// multiple `push` calls.
Expand Down Expand Up @@ -56,7 +63,7 @@ struct VecPushSearcher {
}
impl VecPushSearcher {
fn display_err(&self, cx: &LateContext<'_>) {
let min_pushes_for_extension = match self.init {
let required_pushes_before_extension = match self.init {
_ if self.found == 0 => return,
VecInitKind::WithConstCapacity(x) if x > self.found => return,
VecInitKind::WithConstCapacity(x) => x,
Expand Down Expand Up @@ -110,7 +117,7 @@ impl VecPushSearcher {
});

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

Expand Down

0 comments on commit 94ec421

Please sign in to comment.