Skip to content

Commit 2753f1c

Browse files
committed
Split lint into slow and unsafe vector initalization
1 parent 9b4bc3b commit 2753f1c

File tree

2 files changed

+96
-94
lines changed

2 files changed

+96
-94
lines changed

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,36 @@ use crate::rustc_errors::{Applicability};
3737
declare_clippy_lint! {
3838
pub SLOW_VECTOR_INITIALIZATION,
3939
perf,
40-
"slow or unsafe vector initialization"
40+
"slow vector initialization"
41+
}
42+
43+
/// **What it does:** Checks unsafe vector initialization
44+
///
45+
/// **Why is this bad?** Changing the length of a vector may expose uninitialized memory, which
46+
/// can lead to memory safety issues
47+
///
48+
/// **Known problems:** None.
49+
///
50+
/// **Example:**
51+
/// ```rust
52+
/// let mut vec1 = Vec::with_capacity(len);
53+
/// unsafe { vec1.set_len(len); }
54+
/// ```
55+
declare_clippy_lint! {
56+
pub UNSAFE_VECTOR_INITIALIZATION,
57+
correctness,
58+
"unsafe vector initialization"
4159
}
4260

4361
#[derive(Copy, Clone, Default)]
4462
pub struct Pass;
4563

4664
impl LintPass for Pass {
4765
fn get_lints(&self) -> LintArray {
48-
lint_array!(SLOW_VECTOR_INITIALIZATION)
66+
lint_array!(
67+
SLOW_VECTOR_INITIALIZATION,
68+
UNSAFE_VECTOR_INITIALIZATION,
69+
)
4970
}
5071
}
5172

@@ -96,7 +117,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
96117
len_expr: len_arg,
97118
};
98119

99-
Pass::search_slow_initialization(cx, vi, expr.id, expr.span);
120+
Pass::search_slow_initialization(cx, vi, expr.id);
100121
}
101122
}
102123
}
@@ -117,7 +138,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
117138
len_expr: len_arg,
118139
};
119140

120-
Pass::search_slow_initialization(cx, vi, stmt.node.id(), stmt.span);
141+
Pass::search_slow_initialization(cx, vi, stmt.node.id());
121142
}
122143
}
123144
}
@@ -145,8 +166,7 @@ impl Pass {
145166
fn search_slow_initialization<'tcx>(
146167
cx: &LateContext<'_, 'tcx>,
147168
vec_initialization: VecInitialization<'tcx>,
148-
parent_node: NodeId,
149-
parent_span: Span
169+
parent_node: NodeId
150170
) {
151171
let enclosing_body = get_enclosing_block(cx, parent_node);
152172

@@ -163,30 +183,54 @@ impl Pass {
163183

164184
v.visit_block(enclosing_body.unwrap());
165185

166-
if let Some(ref repeat_expr) = v.slow_expression {
167-
span_lint_and_then(
168-
cx,
169-
SLOW_VECTOR_INITIALIZATION,
170-
parent_span,
171-
"detected slow zero-filling initialization",
172-
|db| {
173-
db.span_suggestion_with_applicability(v.vec_ini.initialization_expr.span, "consider replacing with", "vec![0; ..]".to_string(), Applicability::Unspecified);
174-
175-
match repeat_expr {
176-
InitializationType::Extend(e) => {
177-
db.span_note(e.span, "extended at");
178-
},
179-
InitializationType::Resize(e) => {
180-
db.span_note(e.span, "resized at");
181-
},
182-
InitializationType::UnsafeSetLen(e) => {
183-
db.span_note(e.span, "changed len at");
184-
},
185-
}
186-
}
187-
);
186+
if let Some(ref initialization_expr) = v.slow_expression {
187+
let alloc_span = v.vec_ini.initialization_expr.span;
188+
Pass::lint_initialization(cx, initialization_expr, alloc_span);
188189
}
189190
}
191+
192+
fn lint_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, initialization: &InitializationType<'tcx>, alloc_span: Span) {
193+
match initialization {
194+
InitializationType::UnsafeSetLen(e) =>
195+
Pass::lint_unsafe_initialization(cx, e, alloc_span),
196+
197+
InitializationType::Extend(e) |
198+
InitializationType::Resize(e) =>
199+
Pass::lint_slow_initialization(cx, e, alloc_span),
200+
};
201+
}
202+
203+
fn lint_slow_initialization<'tcx>(
204+
cx: &LateContext<'_, 'tcx>,
205+
slow_fill: &Expr,
206+
alloc_span: Span,
207+
) {
208+
span_lint_and_then(
209+
cx,
210+
SLOW_VECTOR_INITIALIZATION,
211+
slow_fill.span,
212+
"detected slow zero-filling initialization",
213+
|db| {
214+
db.span_suggestion_with_applicability(alloc_span, "consider replacing with", "vec![0; ..]".to_string(), Applicability::Unspecified);
215+
}
216+
);
217+
}
218+
219+
fn lint_unsafe_initialization<'tcx>(
220+
cx: &LateContext<'_, 'tcx>,
221+
slow_fill: &Expr,
222+
alloc_span: Span,
223+
) {
224+
span_lint_and_then(
225+
cx,
226+
UNSAFE_VECTOR_INITIALIZATION,
227+
slow_fill.span,
228+
"detected unsafe vector initialization",
229+
|db| {
230+
db.span_suggestion_with_applicability(alloc_span, "consider replacing with", "vec![0; ..]".to_string(), Applicability::Unspecified);
231+
}
232+
);
233+
}
190234
}
191235

192236
/// SlowInitializationVisitor searches for slow zero filling vector initialization, for the given
Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,73 @@
11
error: detected slow zero-filling initialization
2-
--> $DIR/slow_vector_initialization.rs:22:5
3-
|
4-
22 | let mut vec1 = Vec::with_capacity(len);
5-
| ^^^^^^^^^^^^^^^-----------------------^
6-
| |
7-
| help: consider replacing with: `vec![0; ..]`
8-
|
9-
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings`
10-
note: extended at
112
--> $DIR/slow_vector_initialization.rs:23:5
123
|
4+
22 | let mut vec1 = Vec::with_capacity(len);
5+
| ----------------------- help: consider replacing with: `vec![0; ..]`
136
23 | vec1.extend(repeat(0).take(len));
147
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
|
9+
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings`
1510

1611
error: detected slow zero-filling initialization
17-
--> $DIR/slow_vector_initialization.rs:26:5
18-
|
19-
26 | let mut vec2 = Vec::with_capacity(len - 10);
20-
| ^^^^^^^^^^^^^^^----------------------------^
21-
| |
22-
| help: consider replacing with: `vec![0; ..]`
23-
|
24-
note: extended at
2512
--> $DIR/slow_vector_initialization.rs:27:5
2613
|
14+
26 | let mut vec2 = Vec::with_capacity(len - 10);
15+
| ---------------------------- help: consider replacing with: `vec![0; ..]`
2716
27 | vec2.extend(repeat(0).take(len - 10));
2817
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2918

3019
error: detected slow zero-filling initialization
31-
--> $DIR/slow_vector_initialization.rs:39:5
32-
|
33-
39 | let mut resized_vec = Vec::with_capacity(30);
34-
| ^^^^^^^^^^^^^^^^^^^^^^----------------------^
35-
| |
36-
| help: consider replacing with: `vec![0; ..]`
37-
|
38-
note: resized at
3920
--> $DIR/slow_vector_initialization.rs:42:5
4021
|
22+
39 | let mut resized_vec = Vec::with_capacity(30);
23+
| ---------------------- help: consider replacing with: `vec![0; ..]`
24+
...
4125
42 | resized_vec.resize(30, 0);
4226
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4327

4428
error: detected slow zero-filling initialization
45-
--> $DIR/slow_vector_initialization.rs:40:5
46-
|
47-
40 | let mut extend_vec = Vec::with_capacity(30);
48-
| ^^^^^^^^^^^^^^^^^^^^^----------------------^
49-
| |
50-
| help: consider replacing with: `vec![0; ..]`
51-
|
52-
note: extended at
5329
--> $DIR/slow_vector_initialization.rs:44:5
5430
|
31+
40 | let mut extend_vec = Vec::with_capacity(30);
32+
| ---------------------- help: consider replacing with: `vec![0; ..]`
33+
...
5534
44 | extend_vec.extend(repeat(0).take(30));
5635
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5736

5837
error: detected slow zero-filling initialization
59-
--> $DIR/slow_vector_initialization.rs:50:5
60-
|
61-
50 | let mut vec1 = Vec::with_capacity(len);
62-
| ^^^^^^^^^^^^^^^-----------------------^
63-
| |
64-
| help: consider replacing with: `vec![0; ..]`
65-
|
66-
note: resized at
6738
--> $DIR/slow_vector_initialization.rs:51:5
6839
|
40+
50 | let mut vec1 = Vec::with_capacity(len);
41+
| ----------------------- help: consider replacing with: `vec![0; ..]`
6942
51 | vec1.resize(len, 0);
7043
| ^^^^^^^^^^^^^^^^^^^
7144

7245
error: detected slow zero-filling initialization
73-
--> $DIR/slow_vector_initialization.rs:58:5
74-
|
75-
58 | let mut vec3 = Vec::with_capacity(len - 10);
76-
| ^^^^^^^^^^^^^^^----------------------------^
77-
| |
78-
| help: consider replacing with: `vec![0; ..]`
79-
|
80-
note: resized at
8146
--> $DIR/slow_vector_initialization.rs:59:5
8247
|
48+
58 | let mut vec3 = Vec::with_capacity(len - 10);
49+
| ---------------------------- help: consider replacing with: `vec![0; ..]`
8350
59 | vec3.resize(len - 10, 0);
8451
| ^^^^^^^^^^^^^^^^^^^^^^^^
8552

8653
error: detected slow zero-filling initialization
87-
--> $DIR/slow_vector_initialization.rs:62:5
88-
|
89-
62 | vec1 = Vec::with_capacity(10);
90-
| ^^^^^^^----------------------
91-
| |
92-
| help: consider replacing with: `vec![0; ..]`
93-
|
94-
note: resized at
9554
--> $DIR/slow_vector_initialization.rs:63:5
9655
|
56+
62 | vec1 = Vec::with_capacity(10);
57+
| ---------------------- help: consider replacing with: `vec![0; ..]`
9758
63 | vec1.resize(10, 0);
9859
| ^^^^^^^^^^^^^^^^^^
9960

100-
error: detected slow zero-filling initialization
101-
--> $DIR/slow_vector_initialization.rs:67:5
102-
|
103-
67 | let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
104-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------^
105-
| |
106-
| help: consider replacing with: `vec![0; ..]`
107-
|
108-
note: changed len at
61+
error: detected unsafe vector initialization
10962
--> $DIR/slow_vector_initialization.rs:70:9
11063
|
64+
67 | let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
65+
| ----------------------- help: consider replacing with: `vec![0; ..]`
66+
...
11167
70 | unsafe_vec.set_len(200);
11268
| ^^^^^^^^^^^^^^^^^^^^^^^
69+
|
70+
= note: #[deny(clippy::unsafe_vector_initialization)] on by default
11371

11472
error: aborting due to 8 previous errors
11573

0 commit comments

Comments
 (0)