Skip to content

Commit 5a70d88

Browse files
committed
add vec.capacity() to slow_vec_initialization
1 parent 542d474 commit 5a70d88

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ declare_clippy_lint! {
2828
/// let mut vec1 = Vec::with_capacity(len);
2929
/// vec1.resize(len, 0);
3030
///
31+
/// let mut vec1 = Vec::with_capacity(len);
32+
/// vec1.resize(vec1.capacity(), 0);
33+
///
3134
/// let mut vec2 = Vec::with_capacity(len);
3235
/// vec2.extend(repeat(0).take(len));
3336
///
@@ -210,23 +213,20 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
210213

211214
/// Checks if the given expression is resizing a vector with 0
212215
fn search_slow_resize_filling(&mut self, expr: &'tcx Expr<'_>) {
213-
if_chain! {
214-
if self.initialization_found;
215-
if let ExprKind::MethodCall(path, [self_arg, len_arg, fill_arg], _) = expr.kind;
216-
if path_to_local_id(self_arg, self.vec_alloc.local_id);
217-
if path.ident.name == sym!(resize);
218-
216+
if self.initialization_found
217+
&& let ExprKind::MethodCall(path, [self_arg, len_arg, fill_arg], _) = expr.kind
218+
&& path_to_local_id(self_arg, self.vec_alloc.local_id)
219+
&& path.ident.name == sym!(resize)
219220
// Check that is filled with 0
220-
if let ExprKind::Lit(ref lit) = fill_arg.kind;
221-
if let LitKind::Int(0, _) = lit.node;
222-
223-
// Check that len expression is equals to `with_capacity` expression
224-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
225-
226-
then {
227-
self.slow_expression = Some(InitializationType::Resize(expr));
221+
&& let ExprKind::Lit(ref lit) = fill_arg.kind
222+
&& let LitKind::Int(0, _) = lit.node {
223+
// Check that len expression is equals to `with_capacity` expression
224+
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr) {
225+
self.slow_expression = Some(InitializationType::Resize(expr));
226+
} else if let ExprKind::MethodCall(path, _, _) = len_arg.kind && path.ident.as_str() == "capacity" {
227+
self.slow_expression = Some(InitializationType::Resize(expr));
228+
}
228229
}
229-
}
230230
}
231231

232232
/// Returns `true` if give expression is `repeat(0).take(...)`
@@ -239,12 +239,15 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
239239
if let Some(repeat_expr) = take_args.get(0);
240240
if self.is_repeat_zero(repeat_expr);
241241

242-
// Check that len expression is equals to `with_capacity` expression
243242
if let Some(len_arg) = take_args.get(1);
244-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
245243

246244
then {
247-
return true;
245+
// Check that len expression is equals to `with_capacity` expression
246+
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr) {
247+
return true;
248+
} else if let ExprKind::MethodCall(path, _, _) = len_arg.kind && path.ident.as_str() == "capacity" {
249+
return true;
250+
}
248251
}
249252
}
250253

tests/ui/slow_vector_initialization.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ fn extend_vector() {
1919
// Extend with mismatching expression should not be warned
2020
let mut vec3 = Vec::with_capacity(24322);
2121
vec3.extend(repeat(0).take(2));
22+
23+
let mut vec4 = Vec::with_capacity(len);
24+
vec4.extend(repeat(0).take(vec4.capacity()));
2225
}
2326

2427
fn mixed_extend_resize_vector() {
@@ -48,6 +51,9 @@ fn resize_vector() {
4851
let mut vec3 = Vec::with_capacity(len - 10);
4952
vec3.resize(len - 10, 0);
5053

54+
let mut vec4 = Vec::with_capacity(len);
55+
vec4.resize(vec4.capacity(), 0);
56+
5157
// Reinitialization should be warned
5258
vec1 = Vec::with_capacity(10);
5359
vec1.resize(10, 0);

tests/ui/slow_vector_initialization.stderr

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,60 @@ LL | vec2.extend(repeat(0).take(len - 10));
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: slow zero-filling initialization
20-
--> $DIR/slow_vector_initialization.rs:31:5
20+
--> $DIR/slow_vector_initialization.rs:24:5
21+
|
22+
LL | let mut vec4 = Vec::with_capacity(len);
23+
| ----------------------- help: consider replace allocation with: `vec![0; len]`
24+
LL | vec4.extend(repeat(0).take(vec4.capacity()));
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: slow zero-filling initialization
28+
--> $DIR/slow_vector_initialization.rs:34:5
2129
|
2230
LL | let mut resized_vec = Vec::with_capacity(30);
2331
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
2432
LL | resized_vec.resize(30, 0);
2533
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2634

2735
error: slow zero-filling initialization
28-
--> $DIR/slow_vector_initialization.rs:34:5
36+
--> $DIR/slow_vector_initialization.rs:37:5
2937
|
3038
LL | let mut extend_vec = Vec::with_capacity(30);
3139
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
3240
LL | extend_vec.extend(repeat(0).take(30));
3341
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3442

3543
error: slow zero-filling initialization
36-
--> $DIR/slow_vector_initialization.rs:41:5
44+
--> $DIR/slow_vector_initialization.rs:44:5
3745
|
3846
LL | let mut vec1 = Vec::with_capacity(len);
3947
| ----------------------- help: consider replace allocation with: `vec![0; len]`
4048
LL | vec1.resize(len, 0);
4149
| ^^^^^^^^^^^^^^^^^^^
4250

4351
error: slow zero-filling initialization
44-
--> $DIR/slow_vector_initialization.rs:49:5
52+
--> $DIR/slow_vector_initialization.rs:52:5
4553
|
4654
LL | let mut vec3 = Vec::with_capacity(len - 10);
4755
| ---------------------------- help: consider replace allocation with: `vec![0; len - 10]`
4856
LL | vec3.resize(len - 10, 0);
4957
| ^^^^^^^^^^^^^^^^^^^^^^^^
5058

5159
error: slow zero-filling initialization
52-
--> $DIR/slow_vector_initialization.rs:53:5
60+
--> $DIR/slow_vector_initialization.rs:55:5
61+
|
62+
LL | let mut vec4 = Vec::with_capacity(len);
63+
| ----------------------- help: consider replace allocation with: `vec![0; len]`
64+
LL | vec4.resize(vec4.capacity(), 0);
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
67+
error: slow zero-filling initialization
68+
--> $DIR/slow_vector_initialization.rs:59:5
5369
|
5470
LL | vec1 = Vec::with_capacity(10);
5571
| ---------------------- help: consider replace allocation with: `vec![0; 10]`
5672
LL | vec1.resize(10, 0);
5773
| ^^^^^^^^^^^^^^^^^^
5874

59-
error: aborting due to 7 previous errors
75+
error: aborting due to 9 previous errors
6076

0 commit comments

Comments
 (0)