From ff3ba2f0be4b8dcf8f168314e728894c8b0bd518 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sun, 24 Apr 2022 09:46:17 -0400 Subject: [PATCH] Update `vec_init_then_push` docs --- clippy_lints/src/vec_init_then_push.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/vec_init_then_push.rs b/clippy_lints/src/vec_init_then_push.rs index 5a5dfcfdc8ab..5fc147c19d88 100644 --- a/clippy_lints/src/vec_init_then_push.rs +++ b/clippy_lints/src/vec_init_then_push.rs @@ -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. @@ -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, @@ -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; }