Skip to content

Commit 4899333

Browse files
committed
better suggestion for slow_vector_initialization
1 parent 7a834b5 commit 4899333

File tree

3 files changed

+104
-87
lines changed

3 files changed

+104
-87
lines changed

clippy_lints/src/slow_vector_initialization.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::matching_root_macro_call;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::{
@@ -203,14 +203,18 @@ impl SlowVectorInit {
203203
"len",
204204
);
205205

206-
span_lint_and_then(cx, SLOW_VECTOR_INITIALIZATION, slow_fill.span, msg, |diag| {
207-
diag.span_suggestion(
208-
vec_alloc.allocation_expr.span.source_callsite(),
209-
"consider replacing this with",
210-
format!("vec![0; {len_expr}]"),
211-
Applicability::Unspecified,
212-
);
213-
});
206+
let span_to_replace = slow_fill
207+
.span
208+
.with_lo(vec_alloc.allocation_expr.span.source_callsite().lo());
209+
span_lint_and_sugg(
210+
cx,
211+
SLOW_VECTOR_INITIALIZATION,
212+
span_to_replace,
213+
msg,
214+
"consider replacing this with",
215+
format!("vec![0; {len_expr}]"),
216+
Applicability::Unspecified,
217+
);
214218
}
215219
}
216220

tests/ui/slow_vector_initialization.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ fn extend_vector() {
1111
// Extend with constant expression
1212
let len = 300;
1313
let mut vec1 = Vec::with_capacity(len);
14-
vec1.extend(repeat(0).take(len));
1514
//~^ ERROR: slow zero-filling initialization
1615
//~| NOTE: `-D clippy::slow-vector-initialization` implied by `-D warnings`
16+
vec1.extend(repeat(0).take(len));
1717

1818
// Extend with len expression
1919
let mut vec2 = Vec::with_capacity(len - 10);
20-
vec2.extend(repeat(0).take(len - 10));
2120
//~^ ERROR: slow zero-filling initialization
21+
vec2.extend(repeat(0).take(len - 10));
2222

2323
// Extend with mismatching expression should not be warned
2424
let mut vec3 = Vec::with_capacity(24322);
2525
vec3.extend(repeat(0).take(2));
2626

2727
let mut vec4 = Vec::with_capacity(len);
28-
vec4.extend(repeat(0).take(vec4.capacity()));
2928
//~^ ERROR: slow zero-filling initialization
29+
vec4.extend(repeat(0).take(vec4.capacity()));
3030
}
3131

3232
fn mixed_extend_resize_vector() {
@@ -36,60 +36,60 @@ fn mixed_extend_resize_vector() {
3636

3737
// Slow initialization
3838
let mut resized_vec = Vec::with_capacity(30);
39-
resized_vec.resize(30, 0);
4039
//~^ ERROR: slow zero-filling initialization
40+
resized_vec.resize(30, 0);
4141

4242
let mut extend_vec = Vec::with_capacity(30);
43-
extend_vec.extend(repeat(0).take(30));
4443
//~^ ERROR: slow zero-filling initialization
44+
extend_vec.extend(repeat(0).take(30));
4545
}
4646

4747
fn resize_vector() {
4848
// Resize with constant expression
4949
let len = 300;
5050
let mut vec1 = Vec::with_capacity(len);
51-
vec1.resize(len, 0);
5251
//~^ ERROR: slow zero-filling initialization
52+
vec1.resize(len, 0);
5353

5454
// Resize mismatch len
5555
let mut vec2 = Vec::with_capacity(200);
5656
vec2.resize(10, 0);
5757

5858
// Resize with len expression
5959
let mut vec3 = Vec::with_capacity(len - 10);
60-
vec3.resize(len - 10, 0);
6160
//~^ ERROR: slow zero-filling initialization
61+
vec3.resize(len - 10, 0);
6262

6363
let mut vec4 = Vec::with_capacity(len);
64-
vec4.resize(vec4.capacity(), 0);
6564
//~^ ERROR: slow zero-filling initialization
65+
vec4.resize(vec4.capacity(), 0);
6666

6767
// Reinitialization should be warned
6868
vec1 = Vec::with_capacity(10);
69-
vec1.resize(10, 0);
7069
//~^ ERROR: slow zero-filling initialization
70+
vec1.resize(10, 0);
7171
}
7272

7373
fn from_empty_vec() {
7474
// Resize with constant expression
7575
let len = 300;
7676
let mut vec1 = Vec::new();
77-
vec1.resize(len, 0);
7877
//~^ ERROR: slow zero-filling initialization
78+
vec1.resize(len, 0);
7979

8080
// Resize with len expression
8181
let mut vec3 = Vec::new();
82-
vec3.resize(len - 10, 0);
8382
//~^ ERROR: slow zero-filling initialization
83+
vec3.resize(len - 10, 0);
8484

8585
// Reinitialization should be warned
8686
vec1 = Vec::new();
87-
vec1.resize(10, 0);
8887
//~^ ERROR: slow zero-filling initialization
88+
vec1.resize(10, 0);
8989

9090
vec1 = vec![];
91-
vec1.resize(10, 0);
9291
//~^ ERROR: slow zero-filling initialization
92+
vec1.resize(10, 0);
9393

9494
macro_rules! x {
9595
() => {
+78-65
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,122 @@
11
error: slow zero-filling initialization
2-
--> tests/ui/slow_vector_initialization.rs:14:5
2+
--> tests/ui/slow_vector_initialization.rs:13:20
33
|
4-
LL | let mut vec1 = Vec::with_capacity(len);
5-
| ----------------------- help: consider replacing this with: `vec![0; len]`
6-
LL | vec1.extend(repeat(0).take(len));
7-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | let mut vec1 = Vec::with_capacity(len);
5+
| ____________________^
6+
... |
7+
LL | | vec1.extend(repeat(0).take(len));
8+
| |____________________________________^ help: consider replacing this with: `vec![0; len]`
89
|
910
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings`
1011
= help: to override `-D warnings` add `#[allow(clippy::slow_vector_initialization)]`
1112

1213
error: slow zero-filling initialization
13-
--> tests/ui/slow_vector_initialization.rs:20:5
14+
--> tests/ui/slow_vector_initialization.rs:19:20
1415
|
15-
LL | let mut vec2 = Vec::with_capacity(len - 10);
16-
| ---------------------------- help: consider replacing this with: `vec![0; len - 10]`
17-
LL | vec2.extend(repeat(0).take(len - 10));
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | let mut vec2 = Vec::with_capacity(len - 10);
17+
| ____________________^
18+
LL | |
19+
LL | | vec2.extend(repeat(0).take(len - 10));
20+
| |_________________________________________^ help: consider replacing this with: `vec![0; len - 10]`
1921

2022
error: slow zero-filling initialization
21-
--> tests/ui/slow_vector_initialization.rs:28:5
23+
--> tests/ui/slow_vector_initialization.rs:27:20
2224
|
23-
LL | let mut vec4 = Vec::with_capacity(len);
24-
| ----------------------- help: consider replacing this with: `vec![0; len]`
25-
LL | vec4.extend(repeat(0).take(vec4.capacity()));
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
LL | let mut vec4 = Vec::with_capacity(len);
26+
| ____________________^
27+
LL | |
28+
LL | | vec4.extend(repeat(0).take(vec4.capacity()));
29+
| |________________________________________________^ help: consider replacing this with: `vec![0; len]`
2730

2831
error: slow zero-filling initialization
29-
--> tests/ui/slow_vector_initialization.rs:39:5
32+
--> tests/ui/slow_vector_initialization.rs:38:27
3033
|
31-
LL | let mut resized_vec = Vec::with_capacity(30);
32-
| ---------------------- help: consider replacing this with: `vec![0; 30]`
33-
LL | resized_vec.resize(30, 0);
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
34+
LL | let mut resized_vec = Vec::with_capacity(30);
35+
| ___________________________^
36+
LL | |
37+
LL | | resized_vec.resize(30, 0);
38+
| |_____________________________^ help: consider replacing this with: `vec![0; 30]`
3539

3640
error: slow zero-filling initialization
37-
--> tests/ui/slow_vector_initialization.rs:43:5
41+
--> tests/ui/slow_vector_initialization.rs:42:26
3842
|
39-
LL | let mut extend_vec = Vec::with_capacity(30);
40-
| ---------------------- help: consider replacing this with: `vec![0; 30]`
41-
LL | extend_vec.extend(repeat(0).take(30));
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
LL | let mut extend_vec = Vec::with_capacity(30);
44+
| __________________________^
45+
LL | |
46+
LL | | extend_vec.extend(repeat(0).take(30));
47+
| |_________________________________________^ help: consider replacing this with: `vec![0; 30]`
4348

4449
error: slow zero-filling initialization
45-
--> tests/ui/slow_vector_initialization.rs:51:5
50+
--> tests/ui/slow_vector_initialization.rs:50:20
4651
|
47-
LL | let mut vec1 = Vec::with_capacity(len);
48-
| ----------------------- help: consider replacing this with: `vec![0; len]`
49-
LL | vec1.resize(len, 0);
50-
| ^^^^^^^^^^^^^^^^^^^
52+
LL | let mut vec1 = Vec::with_capacity(len);
53+
| ____________________^
54+
LL | |
55+
LL | | vec1.resize(len, 0);
56+
| |_______________________^ help: consider replacing this with: `vec![0; len]`
5157

5258
error: slow zero-filling initialization
53-
--> tests/ui/slow_vector_initialization.rs:60:5
59+
--> tests/ui/slow_vector_initialization.rs:59:20
5460
|
55-
LL | let mut vec3 = Vec::with_capacity(len - 10);
56-
| ---------------------------- help: consider replacing this with: `vec![0; len - 10]`
57-
LL | vec3.resize(len - 10, 0);
58-
| ^^^^^^^^^^^^^^^^^^^^^^^^
61+
LL | let mut vec3 = Vec::with_capacity(len - 10);
62+
| ____________________^
63+
LL | |
64+
LL | | vec3.resize(len - 10, 0);
65+
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]`
5966

6067
error: slow zero-filling initialization
61-
--> tests/ui/slow_vector_initialization.rs:64:5
68+
--> tests/ui/slow_vector_initialization.rs:63:20
6269
|
63-
LL | let mut vec4 = Vec::with_capacity(len);
64-
| ----------------------- help: consider replacing this with: `vec![0; len]`
65-
LL | vec4.resize(vec4.capacity(), 0);
66-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
LL | let mut vec4 = Vec::with_capacity(len);
71+
| ____________________^
72+
LL | |
73+
LL | | vec4.resize(vec4.capacity(), 0);
74+
| |___________________________________^ help: consider replacing this with: `vec![0; len]`
6775

6876
error: slow zero-filling initialization
69-
--> tests/ui/slow_vector_initialization.rs:69:5
77+
--> tests/ui/slow_vector_initialization.rs:68:12
7078
|
71-
LL | vec1 = Vec::with_capacity(10);
72-
| ---------------------- help: consider replacing this with: `vec![0; 10]`
73-
LL | vec1.resize(10, 0);
74-
| ^^^^^^^^^^^^^^^^^^
79+
LL | vec1 = Vec::with_capacity(10);
80+
| ____________^
81+
LL | |
82+
LL | | vec1.resize(10, 0);
83+
| |______________________^ help: consider replacing this with: `vec![0; 10]`
7584

7685
error: slow zero-filling initialization
77-
--> tests/ui/slow_vector_initialization.rs:77:5
86+
--> tests/ui/slow_vector_initialization.rs:76:20
7887
|
79-
LL | let mut vec1 = Vec::new();
80-
| ---------- help: consider replacing this with: `vec![0; len]`
81-
LL | vec1.resize(len, 0);
82-
| ^^^^^^^^^^^^^^^^^^^
88+
LL | let mut vec1 = Vec::new();
89+
| ____________________^
90+
LL | |
91+
LL | | vec1.resize(len, 0);
92+
| |_______________________^ help: consider replacing this with: `vec![0; len]`
8393

8494
error: slow zero-filling initialization
85-
--> tests/ui/slow_vector_initialization.rs:82:5
95+
--> tests/ui/slow_vector_initialization.rs:81:20
8696
|
87-
LL | let mut vec3 = Vec::new();
88-
| ---------- help: consider replacing this with: `vec![0; len - 10]`
89-
LL | vec3.resize(len - 10, 0);
90-
| ^^^^^^^^^^^^^^^^^^^^^^^^
97+
LL | let mut vec3 = Vec::new();
98+
| ____________________^
99+
LL | |
100+
LL | | vec3.resize(len - 10, 0);
101+
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]`
91102

92103
error: slow zero-filling initialization
93-
--> tests/ui/slow_vector_initialization.rs:87:5
104+
--> tests/ui/slow_vector_initialization.rs:86:12
94105
|
95-
LL | vec1 = Vec::new();
96-
| ---------- help: consider replacing this with: `vec![0; 10]`
97-
LL | vec1.resize(10, 0);
98-
| ^^^^^^^^^^^^^^^^^^
106+
LL | vec1 = Vec::new();
107+
| ____________^
108+
LL | |
109+
LL | | vec1.resize(10, 0);
110+
| |______________________^ help: consider replacing this with: `vec![0; 10]`
99111

100112
error: slow zero-filling initialization
101-
--> tests/ui/slow_vector_initialization.rs:91:5
113+
--> tests/ui/slow_vector_initialization.rs:90:12
102114
|
103-
LL | vec1 = vec![];
104-
| ------ help: consider replacing this with: `vec![0; 10]`
105-
LL | vec1.resize(10, 0);
106-
| ^^^^^^^^^^^^^^^^^^
115+
LL | vec1 = vec![];
116+
| ____________^
117+
LL | |
118+
LL | | vec1.resize(10, 0);
119+
| |______________________^ help: consider replacing this with: `vec![0; 10]`
107120

108121
error: aborting due to 13 previous errors
109122

0 commit comments

Comments
 (0)