-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Vec extend to append #7270
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
Merged
Merged
Vec extend to append #7270
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, LangItem}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::APPEND_INSTEAD_OF_EXTEND; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
if_chain! { | ||
if is_type_diagnostic_item(cx, ty, sym::vec_type); | ||
//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(); | ||
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(); | ||
if is_type_lang_item(cx, src_ty_range, LangItem::RangeFull); | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
APPEND_INSTEAD_OF_EXTEND, | ||
expr.span, | ||
"use of `extend` instead of `append` for adding the full range of a second vector", | ||
"try this", | ||
format!( | ||
"{}.append(&mut {})", | ||
snippet_with_applicability(cx, recv.span, "..", &mut applicability), | ||
snippet_with_applicability(cx, drain_vec.span, "..", &mut applicability) | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// run-rustfix | ||
#![warn(clippy::append_instead_of_extend)] | ||
use std::collections::BinaryHeap; | ||
fn main() { | ||
//gets linted | ||
let mut vec1 = vec![0u8; 1024]; | ||
let mut vec2: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec2.append(&mut vec1); | ||
|
||
let mut vec3 = vec![0u8; 1024]; | ||
let mut vec4: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec4.append(&mut vec3); | ||
|
||
let mut vec11: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec11.append(&mut return_vector()); | ||
|
||
//won't get linted it dosen't move the entire content of a vec into another | ||
let mut test1 = vec![0u8, 10]; | ||
let mut test2: std::vec::Vec<u8> = Vec::new(); | ||
|
||
test2.extend(test1.drain(4..10)); | ||
|
||
let mut vec3 = vec![0u8; 104]; | ||
let mut vec7: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec3.append(&mut vec7); | ||
|
||
let mut vec5 = vec![0u8; 1024]; | ||
let mut vec6: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec5.extend(vec6.drain(..4)); | ||
|
||
let mut vec9: std::vec::Vec<u8> = Vec::new(); | ||
|
||
return_vector().append(&mut vec9); | ||
|
||
//won't get linted because it is not a vec | ||
|
||
let mut heap = BinaryHeap::from(vec![1, 3]); | ||
let mut heap2 = BinaryHeap::from(vec![]); | ||
heap2.extend(heap.drain()) | ||
Valentine-Mario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn return_vector() -> Vec<u8> { | ||
let mut new_vector = vec![]; | ||
|
||
for i in 1..10 { | ||
new_vector.push(i) | ||
} | ||
|
||
new_vector | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// run-rustfix | ||
#![warn(clippy::append_instead_of_extend)] | ||
Valentine-Mario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use std::collections::BinaryHeap; | ||
fn main() { | ||
//gets linted | ||
let mut vec1 = vec![0u8; 1024]; | ||
let mut vec2: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec2.extend(vec1.drain(..)); | ||
Valentine-Mario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let mut vec3 = vec![0u8; 1024]; | ||
let mut vec4: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec4.extend(vec3.drain(..)); | ||
|
||
let mut vec11: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec11.extend(return_vector().drain(..)); | ||
|
||
//won't get linted it dosen't move the entire content of a vec into another | ||
let mut test1 = vec![0u8, 10]; | ||
let mut test2: std::vec::Vec<u8> = Vec::new(); | ||
|
||
test2.extend(test1.drain(4..10)); | ||
|
||
let mut vec3 = vec![0u8; 104]; | ||
let mut vec7: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec3.append(&mut vec7); | ||
|
||
let mut vec5 = vec![0u8; 1024]; | ||
let mut vec6: std::vec::Vec<u8> = Vec::new(); | ||
|
||
vec5.extend(vec6.drain(..4)); | ||
|
||
let mut vec9: std::vec::Vec<u8> = Vec::new(); | ||
|
||
return_vector().append(&mut vec9); | ||
|
||
//won't get linted because it is not a vec | ||
|
||
let mut heap = BinaryHeap::from(vec![1, 3]); | ||
let mut heap2 = BinaryHeap::from(vec![]); | ||
heap2.extend(heap.drain()) | ||
} | ||
Valentine-Mario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn return_vector() -> Vec<u8> { | ||
let mut new_vector = vec![]; | ||
|
||
for i in 1..10 { | ||
new_vector.push(i) | ||
} | ||
|
||
new_vector | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: use of `extend` instead of `append` for adding the full range of a second vector | ||
--> $DIR/append_instead_of_extend.rs:9:5 | ||
| | ||
LL | vec2.extend(vec1.drain(..)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec2.append(&mut vec1)` | ||
| | ||
= note: `-D clippy::append-instead-of-extend` implied by `-D warnings` | ||
|
||
error: use of `extend` instead of `append` for adding the full range of a second vector | ||
--> $DIR/append_instead_of_extend.rs:14:5 | ||
| | ||
LL | vec4.extend(vec3.drain(..)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec4.append(&mut vec3)` | ||
|
||
error: use of `extend` instead of `append` for adding the full range of a second vector | ||
--> $DIR/append_instead_of_extend.rs:18:5 | ||
| | ||
LL | vec11.extend(return_vector().drain(..)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec11.append(&mut return_vector())` | ||
|
||
error: aborting due to 3 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.