add test case for not whole length, move sugg into variable

This commit is contained in:
y21 2023-06-15 14:16:39 +02:00
parent 20ae597ec4
commit 5821fbbc30
4 changed files with 16 additions and 6 deletions

View file

@ -66,17 +66,19 @@ pub(super) fn check(cx: &LateContext<'_>, args: &[Expr<'_>], expr: &Expr<'_>, re
.or_else(|| check_collections(cx, expr_ty, recv_ty_no_refs))
{
let recv = snippet(cx, recv.span, "<expr>");
let sugg = if let ty::Ref(..) = recv_ty.kind() {
format!("std::mem::take({recv})")
} else {
format!("std::mem::take(&mut {recv})")
};
span_lint_and_sugg(
cx,
DRAIN_COLLECT,
expr.span,
&format!("you seem to be trying to move all elements into a new `{typename}`"),
"consider using `mem::take`",
if let ty::Ref(..) = recv_ty.kind() {
format!("std::mem::take({recv})")
} else {
format!("std::mem::take(&mut {recv})")
},
sugg,
Applicability::MachineApplicable,
);
}

View file

@ -3260,7 +3260,7 @@ declare_clippy_lint! {
/// When using `mem::take`, the old collection is replaced with an empty one and ownership of
/// the old collection is returned.
///
/// ### Drawback
/// ### Known issues
/// `mem::take(&mut vec)` is almost equivalent to `vec.drain(..).collect()`, except that
/// it also moves the **capacity**. The user might have explicitly written it this way
/// to keep the capacity on the original `Vec`.

View file

@ -70,4 +70,8 @@ fn string_dont_lint(b: &mut String) -> HashSet<char> {
b.drain(..).collect()
}
fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> {
v.drain(1..).collect()
}
fn main() {}

View file

@ -70,4 +70,8 @@ fn string_dont_lint(b: &mut String) -> HashSet<char> {
b.drain(..).collect()
}
fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> {
v.drain(1..).collect()
}
fn main() {}