-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added lint to check for full range of vector and suggest append
- Loading branch information
1 parent
e4a1e85
commit be79701
Showing
7 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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<'_>) { | ||
if_chain! { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
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"; | ||
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 | ||
let src_ty_range = cx.typeck_results().expr_ty(drain_arg).peel_refs(); | ||
if is_type_lang_item(cx, src_ty_range, LangItem::RangeFull); | ||
let mut applicability = Applicability::MachineApplicable; | ||
then { | ||
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 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 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()) | ||
} | ||
|
||
fn return_vector() -> Vec<u8> { | ||
let mut new_vector = vec![]; | ||
|
||
for i in 1..10 { | ||
new_vector.push(i) | ||
} | ||
|
||
new_vector | ||
} |
This file contains 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.extend(vec1.drain(..)); | ||
|
||
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()) | ||
} | ||
|
||
fn return_vector() -> Vec<u8> { | ||
let mut new_vector = vec![]; | ||
|
||
for i in 1..10 { | ||
new_vector.push(i) | ||
} | ||
|
||
new_vector | ||
} |
This file contains 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 | ||
|