From 2fdb6e4d1ad8b67feb8cca25143c5d61a44dc55b Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 14 Jan 2017 14:39:41 +0100 Subject: [PATCH] deprecate extend_from_slice lint --- README.md | 3 +-- clippy_lints/src/deprecated_lints.rs | 11 ++++++++ clippy_lints/src/lib.rs | 5 +++- clippy_lints/src/methods.rs | 40 ++-------------------------- tests/compile-fail/methods.rs | 25 ----------------- 5 files changed, 18 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 234f84841..01bf489f3 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ transparently: ## Lints -There are 184 lints included in this crate: +There are 183 lints included in this crate: name | default | triggers on -----------------------------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------- @@ -230,7 +230,6 @@ name [explicit_counter_loop](https://github.com/Manishearth/rust-clippy/wiki#explicit_counter_loop) | warn | for-looping with an explicit counter when `_.enumerate()` would do [explicit_into_iter_loop](https://github.com/Manishearth/rust-clippy/wiki#explicit_into_iter_loop) | warn | for-looping over `_.into_iter()` when `_` would do [explicit_iter_loop](https://github.com/Manishearth/rust-clippy/wiki#explicit_iter_loop) | warn | for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do -[extend_from_slice](https://github.com/Manishearth/rust-clippy/wiki#extend_from_slice) | warn | `.extend_from_slice(_)` is a faster way to extend a Vec by a slice [filter_map](https://github.com/Manishearth/rust-clippy/wiki#filter_map) | allow | using combinations of `filter`, `map`, `filter_map` and `flat_map` which can usually be written as a single method call [filter_next](https://github.com/Manishearth/rust-clippy/wiki#filter_next) | warn | using `filter(p).next()`, which is more succinctly expressed as `.find(p)` [float_arithmetic](https://github.com/Manishearth/rust-clippy/wiki#float_arithmetic) | allow | any floating-point arithmetic statement diff --git a/clippy_lints/src/deprecated_lints.rs b/clippy_lints/src/deprecated_lints.rs index abdb6297b..17139f28c 100644 --- a/clippy_lints/src/deprecated_lints.rs +++ b/clippy_lints/src/deprecated_lints.rs @@ -4,6 +4,17 @@ macro_rules! declare_deprecated_lint { } } + +/// **What it does:** Nothing. This lint has been deprecated. +/// +/// **Deprecation reason:** This used to check for `Vec::extend`, which was slower than +/// `Vec::extend_from_slice`. Thanks to specialization, this is no longer true. +declare_deprecated_lint! { + pub EXTEND_FROM_SLICE, + "`.extend_from_slice(_)` is a faster way to extend a Vec by a slice" +} + + /// **What it does:** Nothing. This lint has been deprecated. /// /// **Deprecation reason:** This used to check for `Vec::as_slice`, which was unstable with good diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 9c8f1e789..0d797c567 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -179,6 +179,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { }; let mut store = reg.sess.lint_store.borrow_mut(); + store.register_removed( + "extend_from_slice", + "`.extend_from_slice(_)` is a faster way to extend a Vec by a slice", + ); store.register_removed( "unstable_as_slice", "`Vec::as_slice` has been stabilized in 1.7", @@ -408,7 +412,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { methods::CHARS_NEXT_CMP, methods::CLONE_DOUBLE_REF, methods::CLONE_ON_COPY, - methods::EXTEND_FROM_SLICE, methods::FILTER_NEXT, methods::GET_UNWRAP, methods::ITER_NTH, diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index 21f3f034d..23ceeec78 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -292,24 +292,6 @@ declare_lint! { "using any `*or` method with a function call, which suggests `*or_else`" } -/// **What it does:** Checks for usage of `.extend(s)` on a `Vec` to extend the -/// vector by a slice. -/// -/// **Why is this bad?** Since Rust 1.6, the `extend_from_slice(_)` method is -/// stable and at least for now faster. -/// -/// **Known problems:** None. -/// -/// **Example:** -/// ```rust -/// my_vec.extend(&xs) -/// ``` -declare_lint! { - pub EXTEND_FROM_SLICE, - Warn, - "`.extend_from_slice(_)` is a faster way to extend a Vec by a slice" -} - /// **What it does:** Checks for usage of `.clone()` on a `Copy` type. /// /// **Why is this bad?** The only reason `Copy` types implement `Clone` is for @@ -522,8 +504,7 @@ declare_lint! { impl LintPass for Pass { fn get_lints(&self) -> LintArray { - lint_array!(EXTEND_FROM_SLICE, - OPTION_UNWRAP_USED, + lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, SHOULD_IMPLEMENT_TRAIT, WRONG_SELF_CONVENTION, @@ -844,21 +825,6 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, arg_t } } -fn lint_vec_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) { - let arg_ty = cx.tables.expr_ty(&args[1]); - if let Some(slice) = derefs_to_slice(cx, &args[1], arg_ty) { - span_lint_and_then(cx, - EXTEND_FROM_SLICE, - expr.span, - "use of `extend` to extend a Vec by a slice", - |db| { - db.span_suggestion(expr.span, - "try this", - format!("{}.extend_from_slice({})", snippet(cx, args[0].span, "_"), slice)); - }); - } -} - fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) { let arg = &args[1]; if let Some(arglists) = method_chain_args(arg, &["chars"]) { @@ -885,9 +851,7 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) { fn lint_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) { let (obj_ty, _) = walk_ptrs_ty_depth(cx.tables.expr_ty(&args[0])); - if match_type(cx, obj_ty, &paths::VEC) { - lint_vec_extend(cx, expr, args); - } else if match_type(cx, obj_ty, &paths::STRING) { + if match_type(cx, obj_ty, &paths::STRING) { lint_string_extend(cx, expr, args); } } diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index 0add705d6..0f9a44b61 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -508,31 +508,6 @@ fn starts_with() { //~| SUGGESTION !"".starts_with(' ') } -fn use_extend_from_slice() { - let mut v : Vec<&'static str> = vec![]; - v.extend(&["Hello", "World"]); - //~^ ERROR use of `extend` - //~| HELP try this - //~| SUGGESTION v.extend_from_slice(&["Hello", "World"]); - v.extend(&vec!["Some", "more"]); - //~^ ERROR use of `extend` - //~| HELP try this - //~| SUGGESTION v.extend_from_slice(&vec!["Some", "more"]); - - v.extend(vec!["And", "even", "more"].iter()); - //~^ ERROR use of `extend` - //~| HELP try this - //FIXME: the suggestion if broken because of the macro - let o : Option<&'static str> = None; - v.extend(o); - v.extend(Some("Bye")); - v.extend(vec!["Not", "like", "this"]); - v.extend(["But", "this"].iter()); - //~^ERROR use of `extend - //~| HELP try this - //~| SUGGESTION v.extend_from_slice(&["But", "this"]); -} - fn str_extend_chars() { let abc = "abc"; let def = String::from("def");