Auto merge of #8972 - kyoto7250:use_retain, r=llogiq

feat(new lint): new lint `manual_retain`

close #8097

This PR is  a new  lint implementation.
This lint checks if the `retain` method is available.

Thank you in advance.

changelog: add new ``[`manual_retain`]`` lint
This commit is contained in:
bors 2022-06-27 13:58:26 +00:00
commit eaa03ea911
13 changed files with 859 additions and 4 deletions

View file

@ -3528,6 +3528,7 @@ Released 2018-09-13
[`manual_ok_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_or
[`manual_range_contains`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains
[`manual_rem_euclid`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_rem_euclid
[`manual_retain`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_retain
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
[`manual_split_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_split_once
[`manual_str_repeat`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_str_repeat

View file

@ -5,7 +5,7 @@
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
[There are over 500 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are over 550 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
Lints are divided into categories, each with a default [lint level](https://doc.rust-lang.org/rustc/lints/levels.html).
You can choose how much Clippy is supposed to ~~annoy~~ help you by changing the lint level by category.

View file

@ -6,7 +6,7 @@
A collection of lints to catch common mistakes and improve your
[Rust](https://github.com/rust-lang/rust) code.
[There are over 500 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are over 550 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
Lints are divided into categories, each with a default [lint
level](https://doc.rust-lang.org/rustc/lints/levels.html). You can choose how

View file

@ -136,6 +136,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
LintId::of(manual_bits::MANUAL_BITS),
LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE),
LintId::of(manual_rem_euclid::MANUAL_REM_EUCLID),
LintId::of(manual_retain::MANUAL_RETAIN),
LintId::of(manual_strip::MANUAL_STRIP),
LintId::of(map_clone::MAP_CLONE),
LintId::of(map_unit_fn::OPTION_MAP_UNIT_FN),

View file

@ -257,6 +257,7 @@ store.register_lints(&[
manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE,
manual_ok_or::MANUAL_OK_OR,
manual_rem_euclid::MANUAL_REM_EUCLID,
manual_retain::MANUAL_RETAIN,
manual_strip::MANUAL_STRIP,
map_clone::MAP_CLONE,
map_err_ignore::MAP_ERR_IGNORE,

View file

@ -13,6 +13,7 @@ store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![
LintId::of(loops::MANUAL_MEMCPY),
LintId::of(loops::MISSING_SPIN_LOOP),
LintId::of(loops::NEEDLESS_COLLECT),
LintId::of(manual_retain::MANUAL_RETAIN),
LintId::of(methods::EXPECT_FUN_CALL),
LintId::of(methods::EXTEND_WITH_DRAIN),
LintId::of(methods::ITER_NTH),

View file

@ -283,6 +283,7 @@ mod manual_bits;
mod manual_non_exhaustive;
mod manual_ok_or;
mod manual_rem_euclid;
mod manual_retain;
mod manual_strip;
mod map_clone;
mod map_err_ignore;
@ -914,6 +915,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(read_zero_byte_vec::ReadZeroByteVec));
store.register_late_pass(|| Box::new(default_instead_of_iter_empty::DefaultIterEmpty));
store.register_late_pass(move || Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv)));
store.register_late_pass(move || Box::new(manual_retain::ManualRetain::new(msrv)));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View file

@ -0,0 +1,228 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{get_parent_expr, match_def_path, paths, SpanlessEq};
use clippy_utils::{meets_msrv, msrvs};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::ExprKind::Assign;
use rustc_lint::{LateContext, LateLintPass};
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::sym;
const ACCEPTABLE_METHODS: [&[&str]; 4] = [
&paths::HASHSET_ITER,
&paths::BTREESET_ITER,
&paths::SLICE_INTO,
&paths::VEC_DEQUE_ITER,
];
const ACCEPTABLE_TYPES: [(rustc_span::Symbol, Option<RustcVersion>); 6] = [
(sym::BTreeSet, Some(msrvs::BTREE_SET_RETAIN)),
(sym::BTreeMap, Some(msrvs::BTREE_MAP_RETAIN)),
(sym::HashSet, Some(msrvs::HASH_SET_RETAIN)),
(sym::HashMap, Some(msrvs::HASH_MAP_RETAIN)),
(sym::Vec, None),
(sym::VecDeque, None),
];
declare_clippy_lint! {
/// ### What it does
/// Checks for code to be replaced by `.retain()`.
/// ### Why is this bad?
/// `.retain()` is simpler and avoids needless allocation.
/// ### Example
/// ```rust
/// let mut vec = vec![0, 1, 2];
/// vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
/// vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
/// ```
/// Use instead:
/// ```rust
/// let mut vec = vec![0, 1, 2];
/// vec.retain(|x| x % 2 == 0);
/// ```
#[clippy::version = "1.63.0"]
pub MANUAL_RETAIN,
perf,
"`retain()` is simpler and the same functionalitys"
}
pub struct ManualRetain {
msrv: Option<RustcVersion>,
}
impl ManualRetain {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}
impl_lint_pass!(ManualRetain => [MANUAL_RETAIN]);
impl<'tcx> LateLintPass<'tcx> for ManualRetain {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if let Some(parent_expr) = get_parent_expr(cx, expr)
&& let Assign(left_expr, collect_expr, _) = &parent_expr.kind
&& let hir::ExprKind::MethodCall(seg, _, _) = &collect_expr.kind
&& seg.args.is_none()
&& let hir::ExprKind::MethodCall(_, [target_expr], _) = &collect_expr.kind
&& let Some(collect_def_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id)
&& match_def_path(cx, collect_def_id, &paths::CORE_ITER_COLLECT) {
check_into_iter(cx, parent_expr, left_expr, target_expr, self.msrv);
check_iter(cx, parent_expr, left_expr, target_expr, self.msrv);
check_to_owned(cx, parent_expr, left_expr, target_expr, self.msrv);
}
}
extract_msrv_attr!(LateContext);
}
fn check_into_iter(
cx: &LateContext<'_>,
parent_expr: &hir::Expr<'_>,
left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
msrv: Option<RustcVersion>,
) {
if let hir::ExprKind::MethodCall(_, [into_iter_expr, _], _) = &target_expr.kind
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
&& let hir::ExprKind::MethodCall(_, [struct_expr], _) = &into_iter_expr.kind
&& let Some(into_iter_def_id) = cx.typeck_results().type_dependent_def_id(into_iter_expr.hir_id)
&& match_def_path(cx, into_iter_def_id, &paths::CORE_ITER_INTO_ITER)
&& match_acceptable_type(cx, left_expr, msrv)
&& SpanlessEq::new(cx).eq_expr(left_expr, struct_expr) {
suggest(cx, parent_expr, left_expr, target_expr);
}
}
fn check_iter(
cx: &LateContext<'_>,
parent_expr: &hir::Expr<'_>,
left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
msrv: Option<RustcVersion>,
) {
if let hir::ExprKind::MethodCall(_, [filter_expr], _) = &target_expr.kind
&& let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
&& (match_def_path(cx, copied_def_id, &paths::CORE_ITER_COPIED)
|| match_def_path(cx, copied_def_id, &paths::CORE_ITER_CLONED))
&& let hir::ExprKind::MethodCall(_, [iter_expr, _], _) = &filter_expr.kind
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
&& let hir::ExprKind::MethodCall(_, [struct_expr], _) = &iter_expr.kind
&& let Some(iter_expr_def_id) = cx.typeck_results().type_dependent_def_id(iter_expr.hir_id)
&& match_acceptable_def_path(cx, iter_expr_def_id)
&& match_acceptable_type(cx, left_expr, msrv)
&& SpanlessEq::new(cx).eq_expr(left_expr, struct_expr) {
suggest(cx, parent_expr, left_expr, filter_expr);
}
}
fn check_to_owned(
cx: &LateContext<'_>,
parent_expr: &hir::Expr<'_>,
left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
msrv: Option<RustcVersion>,
) {
if meets_msrv(msrv, msrvs::STRING_RETAIN)
&& let hir::ExprKind::MethodCall(_, [filter_expr], _) = &target_expr.kind
&& let Some(to_owned_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
&& match_def_path(cx, to_owned_def_id, &paths::TO_OWNED_METHOD)
&& let hir::ExprKind::MethodCall(_, [chars_expr, _], _) = &filter_expr.kind
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
&& let hir::ExprKind::MethodCall(_, [str_expr], _) = &chars_expr.kind
&& let Some(chars_expr_def_id) = cx.typeck_results().type_dependent_def_id(chars_expr.hir_id)
&& match_def_path(cx, chars_expr_def_id, &paths::STR_CHARS)
&& let ty = cx.typeck_results().expr_ty(str_expr).peel_refs()
&& is_type_diagnostic_item(cx, ty, sym::String)
&& SpanlessEq::new(cx).eq_expr(left_expr, str_expr) {
suggest(cx, parent_expr, left_expr, filter_expr);
}
}
fn suggest(cx: &LateContext<'_>, parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, filter_expr: &hir::Expr<'_>) {
if let hir::ExprKind::MethodCall(_, [_, closure], _) = filter_expr.kind
&& let hir::ExprKind::Closure{ body, ..} = closure.kind
&& let filter_body = cx.tcx.hir().body(body)
&& let [filter_params] = filter_body.params
&& let Some(sugg) = match filter_params.pat.kind {
hir::PatKind::Binding(_, _, filter_param_ident, None) => {
Some(format!("{}.retain(|{}| {})", snippet(cx, left_expr.span, ".."), filter_param_ident, snippet(cx, filter_body.value.span, "..")))
},
hir::PatKind::Tuple([key_pat, value_pat], _) => {
make_sugg(cx, key_pat, value_pat, left_expr, filter_body)
},
hir::PatKind::Ref(pat, _) => {
match pat.kind {
hir::PatKind::Binding(_, _, filter_param_ident, None) => {
Some(format!("{}.retain(|{}| {})", snippet(cx, left_expr.span, ".."), filter_param_ident, snippet(cx, filter_body.value.span, "..")))
},
_ => None
}
},
_ => None
} {
span_lint_and_sugg(
cx,
MANUAL_RETAIN,
parent_expr.span,
"this expression can be written more simply using `.retain()`",
"consider calling `.retain()` instead",
sugg,
Applicability::MachineApplicable
);
}
}
fn make_sugg(
cx: &LateContext<'_>,
key_pat: &rustc_hir::Pat<'_>,
value_pat: &rustc_hir::Pat<'_>,
left_expr: &hir::Expr<'_>,
filter_body: &hir::Body<'_>,
) -> Option<String> {
match (&key_pat.kind, &value_pat.kind) {
(hir::PatKind::Binding(_, _, key_param_ident, None), hir::PatKind::Binding(_, _, value_param_ident, None)) => {
Some(format!(
"{}.retain(|{}, &mut {}| {})",
snippet(cx, left_expr.span, ".."),
key_param_ident,
value_param_ident,
snippet(cx, filter_body.value.span, "..")
))
},
(hir::PatKind::Binding(_, _, key_param_ident, None), hir::PatKind::Wild) => Some(format!(
"{}.retain(|{}, _| {})",
snippet(cx, left_expr.span, ".."),
key_param_ident,
snippet(cx, filter_body.value.span, "..")
)),
(hir::PatKind::Wild, hir::PatKind::Binding(_, _, value_param_ident, None)) => Some(format!(
"{}.retain(|_, &mut {}| {})",
snippet(cx, left_expr.span, ".."),
value_param_ident,
snippet(cx, filter_body.value.span, "..")
)),
_ => None,
}
}
fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> bool {
ACCEPTABLE_METHODS
.iter()
.any(|&method| match_def_path(cx, collect_def_id, method))
}
fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: Option<RustcVersion>) -> bool {
let expr_ty = cx.typeck_results().expr_ty(expr).peel_refs();
ACCEPTABLE_TYPES.iter().any(|(ty, acceptable_msrv)| {
is_type_diagnostic_item(cx, expr_ty, *ty)
&& acceptable_msrv.map_or(true, |acceptable_msrv| meets_msrv(msrv, acceptable_msrv))
})
}

View file

@ -12,7 +12,7 @@ macro_rules! msrv_aliases {
// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,53,0 { OR_PATTERNS, MANUAL_BITS }
1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN }
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
1,51,0 { BORROW_AS_PTR, UNSIGNED_ABS }
1,50,0 { BOOL_THEN }
@ -30,7 +30,8 @@ msrv_aliases! {
1,34,0 { TRY_FROM }
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
1,28,0 { FROM_BOOL }
1,26,0 { RANGE_INCLUSIVE }
1,26,0 { RANGE_INCLUSIVE, STRING_RETAIN }
1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
1,16,0 { STR_REPEAT }
1,24,0 { IS_ASCII_DIGIT }

View file

@ -21,8 +21,14 @@ pub const ASREF_TRAIT: [&str; 3] = ["core", "convert", "AsRef"];
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
pub const BTREEMAP_ENTRY: [&str; 6] = ["alloc", "collections", "btree", "map", "entry", "Entry"];
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
pub const CORE_ITER_COLLECT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "collect"];
pub const CORE_ITER_CLONED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "cloned"];
pub const CORE_ITER_COPIED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "copied"];
pub const CORE_ITER_FILTER: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "filter"];
pub const CORE_ITER_INTO_ITER: [&str; 6] = ["core", "iter", "traits", "collect", "IntoIterator", "into_iter"];
pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"];
pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"];
pub const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"];
@ -50,6 +56,7 @@ pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWri
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];
pub const HASHSET_ITER: [&str; 6] = ["std", "collections", "hash", "set", "HashSet", "iter"];
#[cfg(feature = "internal")]
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
#[cfg(feature = "internal")]
@ -143,6 +150,7 @@ pub const SLICE_FROM_RAW_PARTS: [&str; 4] = ["core", "slice", "raw", "from_raw_p
pub const SLICE_FROM_RAW_PARTS_MUT: [&str; 4] = ["core", "slice", "raw", "from_raw_parts_mut"];
pub const SLICE_GET: [&str; 4] = ["core", "slice", "<impl [T]>", "get"];
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
pub const SLICE_INTO: [&str; 4] = ["core", "slice", "<impl [T]>", "iter"];
pub const SLICE_ITER: [&str; 4] = ["core", "slice", "iter", "Iter"];
pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"];
pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"];
@ -152,6 +160,7 @@ pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_s
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
pub const STR_BYTES: [&str; 4] = ["core", "str", "<impl str>", "bytes"];
pub const STR_CHARS: [&str; 4] = ["core", "str", "<impl str>", "chars"];
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
@ -177,6 +186,7 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
pub const VEC_DEQUE_ITER: [&str; 5] = ["alloc", "collections", "vec_deque", "VecDeque", "iter"];
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];

View file

@ -0,0 +1,240 @@
// run-rustfix
#![feature(custom_inner_attributes)]
#![warn(clippy::manual_retain)]
#![allow(unused)]
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
fn main() {
binary_heap_retain();
btree_set_retain();
btree_map_retain();
hash_set_retain();
hash_map_retain();
string_retain();
vec_deque_retain();
vec_retain();
_msrv_153();
_msrv_126();
_msrv_118();
}
fn binary_heap_retain() {
// NOTE: Do not lint now, because binary_heap_retain is nighyly API.
// And we need to add a test case for msrv if we update this implmention.
// https://github.com/rust-lang/rust/issues/71503
let mut heap = BinaryHeap::from([1, 2, 3]);
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
// Do not lint, because type conversion is performed
heap = heap.into_iter().filter(|x| x % 2 == 0).collect::<BinaryHeap<i8>>();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect::<BinaryHeap<i8>>();
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect::<BinaryHeap<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: BinaryHeap<i8> = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: BinaryHeap<i8> = heap.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn btree_map_retain() {
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
// Do lint.
btree_map.retain(|k, _| k % 2 == 0);
btree_map.retain(|_, &mut v| v % 2 == 0);
btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));
// Do not lint.
btree_map = btree_map
.into_iter()
.filter(|(x, _)| x % 2 == 0)
.collect::<BTreeMap<i8, i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: BTreeMap<i8, i8> = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
btree_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}
fn btree_set_retain() {
let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
// Do lint.
btree_set.retain(|x| x % 2 == 0);
btree_set.retain(|x| x % 2 == 0);
btree_set.retain(|x| x % 2 == 0);
// Do not lint, because type conversion is performed
btree_set = btree_set
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<BTreeSet<i8>>();
btree_set = btree_set
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<BTreeSet<i8>>();
btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect::<BTreeSet<i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: BTreeSet<i8> = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut bar: BTreeSet<i8> = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn hash_map_retain() {
let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
// Do lint.
hash_map.retain(|k, _| k % 2 == 0);
hash_map.retain(|_, &mut v| v % 2 == 0);
hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));
// Do not lint.
hash_map = hash_map
.into_iter()
.filter(|(x, _)| x % 2 == 0)
.collect::<HashMap<i8, i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: HashMap<i8, i8> = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
hash_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}
fn hash_set_retain() {
let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
// Do lint.
hash_set.retain(|x| x % 2 == 0);
hash_set.retain(|x| x % 2 == 0);
hash_set.retain(|x| x % 2 == 0);
// Do not lint, because type conversion is performed
hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect::<HashSet<i8>>();
hash_set = hash_set
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<HashSet<i8>>();
hash_set = hash_set
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<HashSet<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: HashSet<i8> = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: HashSet<i8> = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|&x| x % 2 == 0).collect();
}
fn string_retain() {
let mut s = String::from("foobar");
// Do lint.
s.retain(|c| c != 'o');
// Do not lint, because this expression is not assign.
let mut bar: String = s.chars().filter(|&c| c != 'o').to_owned().collect();
// Do not lint, because it is an assignment to a different variable.
s = bar.chars().filter(|&c| c != 'o').to_owned().collect();
}
fn vec_retain() {
let mut vec = vec![0, 1, 2];
// Do lint.
vec.retain(|x| x % 2 == 0);
vec.retain(|x| x % 2 == 0);
vec.retain(|x| x % 2 == 0);
// Do not lint, because type conversion is performed
vec = vec.into_iter().filter(|x| x % 2 == 0).collect::<Vec<i8>>();
vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect::<Vec<i8>>();
vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect::<Vec<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: Vec<i8> = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: Vec<i8> = vec.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn vec_deque_retain() {
let mut vec_deque = VecDeque::new();
vec_deque.extend(1..5);
// Do lint.
vec_deque.retain(|x| x % 2 == 0);
vec_deque.retain(|x| x % 2 == 0);
vec_deque.retain(|x| x % 2 == 0);
// Do not lint, because type conversion is performed
vec_deque = vec_deque
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<VecDeque<i8>>();
vec_deque = vec_deque
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<VecDeque<i8>>();
vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect::<VecDeque<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: VecDeque<i8> = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: VecDeque<i8> = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn _msrv_153() {
#![clippy::msrv = "1.52"]
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
}
fn _msrv_126() {
#![clippy::msrv = "1.25"]
let mut s = String::from("foobar");
s = s.chars().filter(|&c| c != 'o').to_owned().collect();
}
fn _msrv_118() {
#![clippy::msrv = "1.17"]
let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}

246
tests/ui/manual_retain.rs Normal file
View file

@ -0,0 +1,246 @@
// run-rustfix
#![feature(custom_inner_attributes)]
#![warn(clippy::manual_retain)]
#![allow(unused)]
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
fn main() {
binary_heap_retain();
btree_set_retain();
btree_map_retain();
hash_set_retain();
hash_map_retain();
string_retain();
vec_deque_retain();
vec_retain();
_msrv_153();
_msrv_126();
_msrv_118();
}
fn binary_heap_retain() {
// NOTE: Do not lint now, because binary_heap_retain is nighyly API.
// And we need to add a test case for msrv if we update this implmention.
// https://github.com/rust-lang/rust/issues/71503
let mut heap = BinaryHeap::from([1, 2, 3]);
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
// Do not lint, because type conversion is performed
heap = heap.into_iter().filter(|x| x % 2 == 0).collect::<BinaryHeap<i8>>();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect::<BinaryHeap<i8>>();
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect::<BinaryHeap<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: BinaryHeap<i8> = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: BinaryHeap<i8> = heap.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn btree_map_retain() {
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
// Do lint.
btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
btree_map = btree_map
.into_iter()
.filter(|(k, v)| (k % 2 == 0) && (v % 2 == 0))
.collect();
// Do not lint.
btree_map = btree_map
.into_iter()
.filter(|(x, _)| x % 2 == 0)
.collect::<BTreeMap<i8, i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: BTreeMap<i8, i8> = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
btree_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}
fn btree_set_retain() {
let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
// Do lint.
btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because type conversion is performed
btree_set = btree_set
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<BTreeSet<i8>>();
btree_set = btree_set
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<BTreeSet<i8>>();
btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect::<BTreeSet<i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: BTreeSet<i8> = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut bar: BTreeSet<i8> = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn hash_map_retain() {
let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
// Do lint.
hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
hash_map = hash_map
.into_iter()
.filter(|(k, v)| (k % 2 == 0) && (v % 2 == 0))
.collect();
// Do not lint.
hash_map = hash_map
.into_iter()
.filter(|(x, _)| x % 2 == 0)
.collect::<HashMap<i8, i8>>();
// Do not lint, because this expression is not assign.
let mut foobar: HashMap<i8, i8> = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
hash_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}
fn hash_set_retain() {
let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
// Do lint.
hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
// Do not lint, because type conversion is performed
hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect::<HashSet<i8>>();
hash_set = hash_set
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<HashSet<i8>>();
hash_set = hash_set
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<HashSet<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: HashSet<i8> = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: HashSet<i8> = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|&x| x % 2 == 0).collect();
}
fn string_retain() {
let mut s = String::from("foobar");
// Do lint.
s = s.chars().filter(|&c| c != 'o').to_owned().collect();
// Do not lint, because this expression is not assign.
let mut bar: String = s.chars().filter(|&c| c != 'o').to_owned().collect();
// Do not lint, because it is an assignment to a different variable.
s = bar.chars().filter(|&c| c != 'o').to_owned().collect();
}
fn vec_retain() {
let mut vec = vec![0, 1, 2];
// Do lint.
vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect();
vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because type conversion is performed
vec = vec.into_iter().filter(|x| x % 2 == 0).collect::<Vec<i8>>();
vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect::<Vec<i8>>();
vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect::<Vec<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: Vec<i8> = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: Vec<i8> = vec.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn vec_deque_retain() {
let mut vec_deque = VecDeque::new();
vec_deque.extend(1..5);
// Do lint.
vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect();
vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because type conversion is performed
vec_deque = vec_deque
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect::<VecDeque<i8>>();
vec_deque = vec_deque
.iter()
.filter(|&x| x % 2 == 0)
.cloned()
.collect::<VecDeque<i8>>();
vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect::<VecDeque<i8>>();
// Do not lint, because this expression is not assign.
let mut bar: VecDeque<i8> = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
let mut foobar: VecDeque<i8> = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
// Do not lint, because it is an assignment to a different variable.
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}
fn _msrv_153() {
#![clippy::msrv = "1.52"]
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
}
fn _msrv_126() {
#![clippy::msrv = "1.25"]
let mut s = String::from("foobar");
s = s.chars().filter(|&c| c != 'o').to_owned().collect();
}
fn _msrv_118() {
#![clippy::msrv = "1.17"]
let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}

View file

@ -0,0 +1,124 @@
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:52:5
|
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`
|
= note: `-D clippy::manual-retain` implied by `-D warnings`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:53:5
|
LL | btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|_, &mut v| v % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:54:5
|
LL | / btree_map = btree_map
LL | | .into_iter()
LL | | .filter(|(k, v)| (k % 2 == 0) && (v % 2 == 0))
LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:76:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:77:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:78:5
|
LL | btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:108:5
|
LL | hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|k, _| k % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:109:5
|
LL | hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|_, &mut v| v % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:110:5
|
LL | / hash_map = hash_map
LL | | .into_iter()
LL | | .filter(|(k, v)| (k % 2 == 0) && (v % 2 == 0))
LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:131:5
|
LL | hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:132:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:133:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:162:5
|
LL | s = s.chars().filter(|&c| c != 'o').to_owned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `s.retain(|c| c != 'o')`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:174:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:175:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:176:5
|
LL | vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:198:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:199:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:200:5
|
LL | vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: aborting due to 19 previous errors