Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug on mutable ref #7533

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clippy_lints/src/methods/extend_with_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
//check source object
if let ExprKind::MethodCall(src_method, _, [drain_vec, drain_arg], _) = &arg.kind;
if src_method.ident.as_str() == "drain";
if let src_ty = cx.typeck_results().expr_ty(drain_vec).peel_refs();
let src_ty = cx.typeck_results().expr_ty(drain_vec);
//check if actual src type is mutable for code suggestion
let immutable = src_ty.is_mutable_ptr();
let src_ty = src_ty.peel_refs();
if is_type_diagnostic_item(cx, src_ty, sym::vec_type);
//check drain range
if let src_ty_range = cx.typeck_results().expr_ty(drain_arg).peel_refs();
Expand All @@ -30,8 +33,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
"use of `extend` instead of `append` for adding the full range of a second vector",
"try this",
format!(
"{}.append(&mut {})",
"{}.append({}{})",
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
if immutable { "" } else { "&mut " },
snippet_with_applicability(cx, drain_vec.span, "..", &mut applicability)
),
applicability,
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/extend_with_drain.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ fn main() {

let mut heap = BinaryHeap::from(vec![1, 3]);
let mut heap2 = BinaryHeap::from(vec![]);
heap2.extend(heap.drain())
heap2.extend(heap.drain());

let mut x = vec![0, 1, 2, 3, 5];
let ref_x = &mut x;
let mut y = Vec::new();
y.append(ref_x);
}

fn return_vector() -> Vec<u8> {
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/extend_with_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ fn main() {

let mut heap = BinaryHeap::from(vec![1, 3]);
let mut heap2 = BinaryHeap::from(vec![]);
heap2.extend(heap.drain())
heap2.extend(heap.drain());

let mut x = vec![0, 1, 2, 3, 5];
let ref_x = &mut x;
let mut y = Vec::new();
y.extend(ref_x.drain(..));
}

fn return_vector() -> Vec<u8> {
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/extend_with_drain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ error: use of `extend` instead of `append` for adding the full range of a second
LL | vec11.extend(return_vector().drain(..));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec11.append(&mut return_vector())`

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

error: aborting due to 4 previous errors