Skip to content

Commit c00210d

Browse files
authored
Merge pull request #3478 from dtolnay/setlen
Remove unsafe_vector_initialization lint
2 parents 13438b6 + e632a19 commit c00210d

6 files changed

+39
-97
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
99

10-
[There are 290 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 289 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

1212
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1313

clippy_lints/src/deprecated_lints.rs

+11
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,14 @@ declare_deprecated_lint! {
112112
pub IF_LET_REDUNDANT_PATTERN_MATCHING,
113113
"this lint has been changed to redundant_pattern_matching"
114114
}
115+
116+
/// **What it does:** Nothing. This lint has been deprecated.
117+
///
118+
/// **Deprecation reason:** This lint used to suggest replacing `let mut vec =
119+
/// Vec::with_capacity(n); vec.set_len(n);` with `let vec = vec![0; n];`. The
120+
/// replacement has very different performance characteristics so the lint is
121+
/// deprecated.
122+
declare_deprecated_lint! {
123+
pub UNSAFE_VECTOR_INITIALIZATION,
124+
"the replacement suggested by this lint had substantially different behavior"
125+
}

clippy_lints/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
330330
"if_let_redundant_pattern_matching",
331331
"this lint has been changed to redundant_pattern_matching",
332332
);
333+
store.register_removed(
334+
"unsafe_vector_initialization",
335+
"the replacement suggested by this lint had substantially different behavior",
336+
);
333337
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
334338

335339
reg.register_late_lint_pass(box serde_api::Serde);
@@ -727,7 +731,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
727731
returns::UNUSED_UNIT,
728732
serde_api::SERDE_API_MISUSE,
729733
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
730-
slow_vector_initialization::UNSAFE_VECTOR_INITIALIZATION,
731734
strings::STRING_LIT_AS_BYTES,
732735
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
733736
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -973,7 +976,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
973976
ranges::ITERATOR_STEP_BY_ZERO,
974977
regex::INVALID_REGEX,
975978
serde_api::SERDE_API_MISUSE,
976-
slow_vector_initialization::UNSAFE_VECTOR_INITIALIZATION,
977979
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
978980
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
979981
swap::ALMOST_SWAPPED,

clippy_lints/src/slow_vector_initialization.rs

+1-52
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,12 @@ declare_clippy_lint! {
3939
"slow vector initialization"
4040
}
4141

42-
/// **What it does:** Checks unsafe vector initialization
43-
///
44-
/// **Why is this bad?** Changing the length of a vector may expose uninitialized memory, which
45-
/// can lead to memory safety issues
46-
///
47-
/// **Known problems:** None.
48-
///
49-
/// **Example:**
50-
/// ```rust
51-
/// let mut vec1 = Vec::with_capacity(len);
52-
/// unsafe {
53-
/// vec1.set_len(len);
54-
/// }
55-
/// ```
56-
declare_clippy_lint! {
57-
pub UNSAFE_VECTOR_INITIALIZATION,
58-
correctness,
59-
"unsafe vector initialization"
60-
}
61-
6242
#[derive(Copy, Clone, Default)]
6343
pub struct Pass;
6444

6545
impl LintPass for Pass {
6646
fn get_lints(&self) -> LintArray {
67-
lint_array!(SLOW_VECTOR_INITIALIZATION, UNSAFE_VECTOR_INITIALIZATION,)
47+
lint_array!(SLOW_VECTOR_INITIALIZATION,)
6848
}
6949
}
7050

@@ -90,9 +70,6 @@ enum InitializationType<'tcx> {
9070

9171
/// Resize is a slow initialization with the form `vec.resize(.., 0)`
9272
Resize(&'tcx Expr),
93-
94-
/// UnsafeSetLen is a slow initialization with the form `vec.set_len(..)`
95-
UnsafeSetLen(&'tcx Expr),
9673
}
9774

9875
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
@@ -188,14 +165,6 @@ impl Pass {
188165
vec_alloc: &VecAllocation<'_>,
189166
) {
190167
match initialization {
191-
InitializationType::UnsafeSetLen(e) => Self::emit_lint(
192-
cx,
193-
e,
194-
vec_alloc,
195-
"unsafe vector initialization",
196-
UNSAFE_VECTOR_INITIALIZATION,
197-
),
198-
199168
InitializationType::Extend(e) | InitializationType::Resize(e) => Self::emit_lint(
200169
cx,
201170
e,
@@ -282,25 +251,6 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
282251
}
283252
}
284253

285-
/// Checks if the given expression is using `set_len` to initialize the vector
286-
fn search_unsafe_set_len(&mut self, expr: &'tcx Expr) {
287-
if_chain! {
288-
if self.initialization_found;
289-
if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
290-
if let ExprKind::Path(ref qpath_subj) = args[0].node;
291-
if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
292-
if path.ident.name == "set_len";
293-
if let Some(ref len_arg) = args.get(1);
294-
295-
// Check that len expression is equals to `with_capacity` expression
296-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
297-
298-
then {
299-
self.slow_expression = Some(InitializationType::UnsafeSetLen(expr));
300-
}
301-
}
302-
}
303-
304254
/// Returns `true` if give expression is `repeat(0).take(...)`
305255
fn is_repeat_take(&self, expr: &Expr) -> bool {
306256
if_chain! {
@@ -349,7 +299,6 @@ impl<'a, 'tcx> Visitor<'tcx> for VectorInitializationVisitor<'a, 'tcx> {
349299
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => {
350300
self.search_slow_extend_filling(expr);
351301
self.search_slow_resize_filling(expr);
352-
self.search_unsafe_set_len(expr);
353302
},
354303
_ => (),
355304
}

tests/ui/slow_vector_initialization.rs

-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn main() {
1313
resize_vector();
1414
extend_vector();
1515
mixed_extend_resize_vector();
16-
unsafe_vector();
1716
}
1817

1918
fn extend_vector() {
@@ -63,14 +62,6 @@ fn resize_vector() {
6362
vec1.resize(10, 0);
6463
}
6564

66-
fn unsafe_vector() {
67-
let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
68-
69-
unsafe {
70-
unsafe_vec.set_len(200);
71-
}
72-
}
73-
7465
fn do_stuff(vec: &mut Vec<u8>) {
7566

7667
}
+22-33
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,60 @@
11
error: slow zero-filling initialization
2-
--> $DIR/slow_vector_initialization.rs:23:5
2+
--> $DIR/slow_vector_initialization.rs:22:5
33
|
4-
22 | let mut vec1 = Vec::with_capacity(len);
4+
21 | let mut vec1 = Vec::with_capacity(len);
55
| ----------------------- help: consider replace allocation with: `vec![0; len]`
6-
23 | vec1.extend(repeat(0).take(len));
6+
22 | vec1.extend(repeat(0).take(len));
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
|
99
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings`
1010

1111
error: slow zero-filling initialization
12-
--> $DIR/slow_vector_initialization.rs:27:5
12+
--> $DIR/slow_vector_initialization.rs:26:5
1313
|
14-
26 | let mut vec2 = Vec::with_capacity(len - 10);
14+
25 | let mut vec2 = Vec::with_capacity(len - 10);
1515
| ---------------------------- help: consider replace allocation with: `vec![0; len - 10]`
16-
27 | vec2.extend(repeat(0).take(len - 10));
16+
26 | vec2.extend(repeat(0).take(len - 10));
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: slow zero-filling initialization
20-
--> $DIR/slow_vector_initialization.rs:41:5
20+
--> $DIR/slow_vector_initialization.rs:40:5
2121
|
22-
40 | let mut resized_vec = Vec::with_capacity(30);
22+
39 | let mut resized_vec = Vec::with_capacity(30);
2323
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
24-
41 | resized_vec.resize(30, 0);
24+
40 | resized_vec.resize(30, 0);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: slow zero-filling initialization
28-
--> $DIR/slow_vector_initialization.rs:44:5
28+
--> $DIR/slow_vector_initialization.rs:43:5
2929
|
30-
43 | let mut extend_vec = Vec::with_capacity(30);
30+
42 | let mut extend_vec = Vec::with_capacity(30);
3131
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
32-
44 | extend_vec.extend(repeat(0).take(30));
32+
43 | extend_vec.extend(repeat(0).take(30));
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434

3535
error: slow zero-filling initialization
36-
--> $DIR/slow_vector_initialization.rs:51:5
36+
--> $DIR/slow_vector_initialization.rs:50:5
3737
|
38-
50 | let mut vec1 = Vec::with_capacity(len);
38+
49 | let mut vec1 = Vec::with_capacity(len);
3939
| ----------------------- help: consider replace allocation with: `vec![0; len]`
40-
51 | vec1.resize(len, 0);
40+
50 | vec1.resize(len, 0);
4141
| ^^^^^^^^^^^^^^^^^^^
4242

4343
error: slow zero-filling initialization
44-
--> $DIR/slow_vector_initialization.rs:59:5
44+
--> $DIR/slow_vector_initialization.rs:58:5
4545
|
46-
58 | let mut vec3 = Vec::with_capacity(len - 10);
46+
57 | let mut vec3 = Vec::with_capacity(len - 10);
4747
| ---------------------------- help: consider replace allocation with: `vec![0; len - 10]`
48-
59 | vec3.resize(len - 10, 0);
48+
58 | vec3.resize(len - 10, 0);
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: slow zero-filling initialization
52-
--> $DIR/slow_vector_initialization.rs:63:5
52+
--> $DIR/slow_vector_initialization.rs:62:5
5353
|
54-
62 | vec1 = Vec::with_capacity(10);
54+
61 | vec1 = Vec::with_capacity(10);
5555
| ---------------------- help: consider replace allocation with: `vec![0; 10]`
56-
63 | vec1.resize(10, 0);
56+
62 | vec1.resize(10, 0);
5757
| ^^^^^^^^^^^^^^^^^^
5858

59-
error: unsafe vector initialization
60-
--> $DIR/slow_vector_initialization.rs:70:9
61-
|
62-
67 | let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
63-
| ----------------------- help: consider replace allocation with: `vec![0; 200]`
64-
...
65-
70 | unsafe_vec.set_len(200);
66-
| ^^^^^^^^^^^^^^^^^^^^^^^
67-
|
68-
= note: #[deny(clippy::unsafe_vector_initialization)] on by default
69-
70-
error: aborting due to 8 previous errors
59+
error: aborting due to 7 previous errors
7160

0 commit comments

Comments
 (0)