Skip to content

Commit a0e71e5

Browse files
committed
Auto merge of rust-lang#7533 - Valentine-Mario:vec_extend_to_append, r=xFrednet
fix bug on mutable ref fixes: rust-lang#7524 This PR is related to issue rust-lang#7524 changelog: [`extend_with_drain`] Improve code suggestion for mutable and immutable references
2 parents 2dbf0c1 + 8a4ffb8 commit a0e71e5

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

clippy_lints/src/methods/extend_with_drain.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
1616
//check source object
1717
if let ExprKind::MethodCall(src_method, _, [drain_vec, drain_arg], _) = &arg.kind;
1818
if src_method.ident.as_str() == "drain";
19-
if let src_ty = cx.typeck_results().expr_ty(drain_vec).peel_refs();
19+
let src_ty = cx.typeck_results().expr_ty(drain_vec);
20+
//check if actual src type is mutable for code suggestion
21+
let immutable = src_ty.is_mutable_ptr();
22+
let src_ty = src_ty.peel_refs();
2023
if is_type_diagnostic_item(cx, src_ty, sym::vec_type);
2124
//check drain range
2225
if let src_ty_range = cx.typeck_results().expr_ty(drain_arg).peel_refs();
@@ -30,8 +33,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
3033
"use of `extend` instead of `append` for adding the full range of a second vector",
3134
"try this",
3235
format!(
33-
"{}.append(&mut {})",
36+
"{}.append({}{})",
3437
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
38+
if immutable { "" } else { "&mut " },
3539
snippet_with_applicability(cx, drain_vec.span, "..", &mut applicability)
3640
),
3741
applicability,

tests/ui/extend_with_drain.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ fn main() {
4141

4242
let mut heap = BinaryHeap::from(vec![1, 3]);
4343
let mut heap2 = BinaryHeap::from(vec![]);
44-
heap2.extend(heap.drain())
44+
heap2.extend(heap.drain());
45+
46+
let mut x = vec![0, 1, 2, 3, 5];
47+
let ref_x = &mut x;
48+
let mut y = Vec::new();
49+
y.append(ref_x);
4550
}
4651

4752
fn return_vector() -> Vec<u8> {

tests/ui/extend_with_drain.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ fn main() {
4141

4242
let mut heap = BinaryHeap::from(vec![1, 3]);
4343
let mut heap2 = BinaryHeap::from(vec![]);
44-
heap2.extend(heap.drain())
44+
heap2.extend(heap.drain());
45+
46+
let mut x = vec![0, 1, 2, 3, 5];
47+
let ref_x = &mut x;
48+
let mut y = Vec::new();
49+
y.extend(ref_x.drain(..));
4550
}
4651

4752
fn return_vector() -> Vec<u8> {

tests/ui/extend_with_drain.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: use of `extend` instead of `append` for adding the full range of a second
1818
LL | vec11.extend(return_vector().drain(..));
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec11.append(&mut return_vector())`
2020

21-
error: aborting due to 3 previous errors
21+
error: use of `extend` instead of `append` for adding the full range of a second vector
22+
--> $DIR/extend_with_drain.rs:49:5
23+
|
24+
LL | y.extend(ref_x.drain(..));
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `y.append(ref_x)`
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)