Skip to content

Commit 5821fbb

Browse files
committed
add test case for not whole length, move sugg into variable
1 parent 20ae597 commit 5821fbb

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

clippy_lints/src/methods/drain_collect.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ pub(super) fn check(cx: &LateContext<'_>, args: &[Expr<'_>], expr: &Expr<'_>, re
6666
.or_else(|| check_collections(cx, expr_ty, recv_ty_no_refs))
6767
{
6868
let recv = snippet(cx, recv.span, "<expr>");
69+
let sugg = if let ty::Ref(..) = recv_ty.kind() {
70+
format!("std::mem::take({recv})")
71+
} else {
72+
format!("std::mem::take(&mut {recv})")
73+
};
74+
6975
span_lint_and_sugg(
7076
cx,
7177
DRAIN_COLLECT,
7278
expr.span,
7379
&format!("you seem to be trying to move all elements into a new `{typename}`"),
7480
"consider using `mem::take`",
75-
if let ty::Ref(..) = recv_ty.kind() {
76-
format!("std::mem::take({recv})")
77-
} else {
78-
format!("std::mem::take(&mut {recv})")
79-
},
81+
sugg,
8082
Applicability::MachineApplicable,
8183
);
8284
}

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3260,7 +3260,7 @@ declare_clippy_lint! {
32603260
/// When using `mem::take`, the old collection is replaced with an empty one and ownership of
32613261
/// the old collection is returned.
32623262
///
3263-
/// ### Drawback
3263+
/// ### Known issues
32643264
/// `mem::take(&mut vec)` is almost equivalent to `vec.drain(..).collect()`, except that
32653265
/// it also moves the **capacity**. The user might have explicitly written it this way
32663266
/// to keep the capacity on the original `Vec`.

tests/ui/drain_collect.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ fn string_dont_lint(b: &mut String) -> HashSet<char> {
7070
b.drain(..).collect()
7171
}
7272

73+
fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> {
74+
v.drain(1..).collect()
75+
}
76+
7377
fn main() {}

tests/ui/drain_collect.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ fn string_dont_lint(b: &mut String) -> HashSet<char> {
7070
b.drain(..).collect()
7171
}
7272

73+
fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> {
74+
v.drain(1..).collect()
75+
}
76+
7377
fn main() {}

0 commit comments

Comments
 (0)