diff --git a/README.md b/README.md index 92bb4586688..0d83224e3f6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 290 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 289 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/clippy_lints/src/deprecated_lints.rs b/clippy_lints/src/deprecated_lints.rs index 904036fe888..17bef09164b 100644 --- a/clippy_lints/src/deprecated_lints.rs +++ b/clippy_lints/src/deprecated_lints.rs @@ -112,3 +112,14 @@ declare_deprecated_lint! { pub IF_LET_REDUNDANT_PATTERN_MATCHING, "this lint has been changed to redundant_pattern_matching" } + +/// **What it does:** Nothing. This lint has been deprecated. +/// +/// **Deprecation reason:** This lint used to suggest replacing `let mut vec = +/// Vec::with_capacity(n); vec.set_len(n);` with `let vec = vec![0; n];`. The +/// replacement has very different performance characteristics so the lint is +/// deprecated. +declare_deprecated_lint! { + pub UNSAFE_VECTOR_INITIALIZATION, + "the replacement suggested by this lint had substantially different behavior" +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index c2e625f3d52..f08294c8397 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -330,6 +330,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { "if_let_redundant_pattern_matching", "this lint has been changed to redundant_pattern_matching", ); + store.register_removed( + "unsafe_vector_initialization", + "the replacement suggested by this lint had substantially different behavior", + ); // end deprecated lints, do not remove this comment, it’s used in `update_lints` reg.register_late_lint_pass(box serde_api::Serde); @@ -495,7 +499,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { panic_unimplemented::UNIMPLEMENTED, shadow::SHADOW_REUSE, shadow::SHADOW_SAME, - slow_vector_initialization::UNSAFE_VECTOR_INITIALIZATION, strings::STRING_ADD, write::PRINT_STDOUT, write::USE_DEBUG, diff --git a/clippy_lints/src/slow_vector_initialization.rs b/clippy_lints/src/slow_vector_initialization.rs index 2964bb6fac7..0ec6fc0d0d1 100644 --- a/clippy_lints/src/slow_vector_initialization.rs +++ b/clippy_lints/src/slow_vector_initialization.rs @@ -39,32 +39,12 @@ declare_clippy_lint! { "slow vector initialization" } -/// **What it does:** Checks unsafe vector initialization -/// -/// **Why is this bad?** Changing the length of a vector may expose uninitialized memory, which -/// can lead to memory safety issues -/// -/// **Known problems:** None. -/// -/// **Example:** -/// ```rust -/// let mut vec1 = Vec::with_capacity(len); -/// unsafe { -/// vec1.set_len(len); -/// } -/// ``` -declare_clippy_lint! { - pub UNSAFE_VECTOR_INITIALIZATION, - restriction, - "unsafe vector initialization" -} - #[derive(Copy, Clone, Default)] pub struct Pass; impl LintPass for Pass { fn get_lints(&self) -> LintArray { - lint_array!(SLOW_VECTOR_INITIALIZATION, UNSAFE_VECTOR_INITIALIZATION,) + lint_array!(SLOW_VECTOR_INITIALIZATION,) } } @@ -90,9 +70,6 @@ enum InitializationType<'tcx> { /// Resize is a slow initialization with the form `vec.resize(.., 0)` Resize(&'tcx Expr), - - /// UnsafeSetLen is a slow initialization with the form `vec.set_len(..)` - UnsafeSetLen(&'tcx Expr), } impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { @@ -188,14 +165,6 @@ impl Pass { vec_alloc: &VecAllocation<'_>, ) { match initialization { - InitializationType::UnsafeSetLen(e) => Self::emit_lint( - cx, - e, - vec_alloc, - "unsafe vector initialization", - UNSAFE_VECTOR_INITIALIZATION, - ), - InitializationType::Extend(e) | InitializationType::Resize(e) => Self::emit_lint( cx, e, @@ -282,25 +251,6 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> { } } - /// Checks if the given expression is using `set_len` to initialize the vector - fn search_unsafe_set_len(&mut self, expr: &'tcx Expr) { - if_chain! { - if self.initialization_found; - if let ExprKind::MethodCall(ref path, _, ref args) = expr.node; - if let ExprKind::Path(ref qpath_subj) = args[0].node; - if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]); - if path.ident.name == "set_len"; - if let Some(ref len_arg) = args.get(1); - - // Check that len expression is equals to `with_capacity` expression - if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr); - - then { - self.slow_expression = Some(InitializationType::UnsafeSetLen(expr)); - } - } - } - /// Returns `true` if give expression is `repeat(0).take(...)` fn is_repeat_take(&self, expr: &Expr) -> bool { if_chain! { @@ -349,7 +299,6 @@ impl<'a, 'tcx> Visitor<'tcx> for VectorInitializationVisitor<'a, 'tcx> { StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => { self.search_slow_extend_filling(expr); self.search_slow_resize_filling(expr); - self.search_unsafe_set_len(expr); }, _ => (), } diff --git a/tests/ui/slow_vector_initialization.rs b/tests/ui/slow_vector_initialization.rs index aa80fc87aa2..5364bf70ff0 100644 --- a/tests/ui/slow_vector_initialization.rs +++ b/tests/ui/slow_vector_initialization.rs @@ -7,15 +7,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![warn(clippy::unsafe_vector_initialization)] - use std::iter::repeat; fn main() { resize_vector(); extend_vector(); mixed_extend_resize_vector(); - unsafe_vector(); } fn extend_vector() { @@ -65,14 +62,6 @@ fn resize_vector() { vec1.resize(10, 0); } -fn unsafe_vector() { - let mut unsafe_vec: Vec = Vec::with_capacity(200); - - unsafe { - unsafe_vec.set_len(200); - } -} - fn do_stuff(vec: &mut Vec) { } diff --git a/tests/ui/slow_vector_initialization.stderr b/tests/ui/slow_vector_initialization.stderr index bc05dd6e1e5..f45c3b48b1b 100644 --- a/tests/ui/slow_vector_initialization.stderr +++ b/tests/ui/slow_vector_initialization.stderr @@ -1,71 +1,60 @@ error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:25:5 + --> $DIR/slow_vector_initialization.rs:22:5 | -24 | let mut vec1 = Vec::with_capacity(len); +21 | let mut vec1 = Vec::with_capacity(len); | ----------------------- help: consider replace allocation with: `vec![0; len]` -25 | vec1.extend(repeat(0).take(len)); +22 | vec1.extend(repeat(0).take(len)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::slow-vector-initialization` implied by `-D warnings` error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:29:5 + --> $DIR/slow_vector_initialization.rs:26:5 | -28 | let mut vec2 = Vec::with_capacity(len - 10); +25 | let mut vec2 = Vec::with_capacity(len - 10); | ---------------------------- help: consider replace allocation with: `vec![0; len - 10]` -29 | vec2.extend(repeat(0).take(len - 10)); +26 | vec2.extend(repeat(0).take(len - 10)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: slow zero-filling initialization + --> $DIR/slow_vector_initialization.rs:40:5 + | +39 | let mut resized_vec = Vec::with_capacity(30); + | ---------------------- help: consider replace allocation with: `vec![0; 30]` +40 | resized_vec.resize(30, 0); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + error: slow zero-filling initialization --> $DIR/slow_vector_initialization.rs:43:5 | -42 | let mut resized_vec = Vec::with_capacity(30); - | ---------------------- help: consider replace allocation with: `vec![0; 30]` -43 | resized_vec.resize(30, 0); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:46:5 - | -45 | let mut extend_vec = Vec::with_capacity(30); +42 | let mut extend_vec = Vec::with_capacity(30); | ---------------------- help: consider replace allocation with: `vec![0; 30]` -46 | extend_vec.extend(repeat(0).take(30)); +43 | extend_vec.extend(repeat(0).take(30)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:53:5 + --> $DIR/slow_vector_initialization.rs:50:5 | -52 | let mut vec1 = Vec::with_capacity(len); +49 | let mut vec1 = Vec::with_capacity(len); | ----------------------- help: consider replace allocation with: `vec![0; len]` -53 | vec1.resize(len, 0); +50 | vec1.resize(len, 0); | ^^^^^^^^^^^^^^^^^^^ error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:61:5 + --> $DIR/slow_vector_initialization.rs:58:5 | -60 | let mut vec3 = Vec::with_capacity(len - 10); +57 | let mut vec3 = Vec::with_capacity(len - 10); | ---------------------------- help: consider replace allocation with: `vec![0; len - 10]` -61 | vec3.resize(len - 10, 0); +58 | vec3.resize(len - 10, 0); | ^^^^^^^^^^^^^^^^^^^^^^^^ error: slow zero-filling initialization - --> $DIR/slow_vector_initialization.rs:65:5 + --> $DIR/slow_vector_initialization.rs:62:5 | -64 | vec1 = Vec::with_capacity(10); +61 | vec1 = Vec::with_capacity(10); | ---------------------- help: consider replace allocation with: `vec![0; 10]` -65 | vec1.resize(10, 0); +62 | vec1.resize(10, 0); | ^^^^^^^^^^^^^^^^^^ -error: unsafe vector initialization - --> $DIR/slow_vector_initialization.rs:72:9 - | -69 | let mut unsafe_vec: Vec = Vec::with_capacity(200); - | ----------------------- help: consider replace allocation with: `vec![0; 200]` -... -72 | unsafe_vec.set_len(200); - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::unsafe-vector-initialization` implied by `-D warnings` - -error: aborting due to 8 previous errors +error: aborting due to 7 previous errors