mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
add test case for not whole length, move sugg into variable
This commit is contained in:
parent
20ae597ec4
commit
5821fbbc30
4 changed files with 16 additions and 6 deletions
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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`.
|
||||
|
|
|
@ -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() {}
|
||||
|
|
|
@ -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() {}
|
||||
|
|
Loading…
Reference in a new issue