Finish tests - add rustfix

This commit is contained in:
bluthej 2023-03-21 22:46:59 +01:00
parent 1d0acce984
commit e8ec242a61
4 changed files with 69 additions and 2 deletions

View file

@ -10,8 +10,6 @@ use rustc_span::Span;
use super::CLEAR_WITH_DRAIN;
// TODO: Adjust the parameters as necessary
// see clippy_lints/src/methods/mod.rs to add call to this check in `check_methods`
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, arg: &Expr<'_>) {
let ty = cx.typeck_results().expr_ty(recv);
if is_type_diagnostic_item(cx, ty, sym::Vec) && let Some(range) = Range::hir(arg) && is_range_full(cx, recv, range)

View file

@ -0,0 +1,40 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::clear_with_drain)]
fn range() {
let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]);
let iter = u.drain(0..u.len()); // Yay
v.clear(); // Nay
}
fn range_from() {
let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]);
let iter = u.drain(0..); // Yay
v.clear(); // Nay
}
fn range_full() {
let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]);
let iter = u.drain(..); // Yay
v.clear(); // Nay
}
fn range_to() {
let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]);
let iter = u.drain(..u.len()); // Yay
v.clear(); // Nay
}
fn partial_drains() {
let mut v = vec![1, 2, 3];
v.drain(1..); // Yay
let mut v = vec![1, 2, 3];
v.drain(..v.len() - 1); // Yay
let mut v = vec![1, 2, 3];
v.drain(1..v.len() - 1); // Yay
}
fn main() {}

View file

@ -1,3 +1,4 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::clear_with_drain)]

View file

@ -0,0 +1,28 @@
error: `drain` used to clear a `Vec`
--> $DIR/clear_with_drain.rs:8:7
|
LL | v.drain(0..v.len()); // Nay
| ^^^^^^^^^^^^^^^^^ help: try: `clear()`
|
= note: `-D clippy::clear-with-drain` implied by `-D warnings`
error: `drain` used to clear a `Vec`
--> $DIR/clear_with_drain.rs:14:7
|
LL | v.drain(0..); // Nay
| ^^^^^^^^^^ help: try: `clear()`
error: `drain` used to clear a `Vec`
--> $DIR/clear_with_drain.rs:20:7
|
LL | v.drain(..); // Nay
| ^^^^^^^^^ help: try: `clear()`
error: `drain` used to clear a `Vec`
--> $DIR/clear_with_drain.rs:26:7
|
LL | v.drain(..v.len()); // Nay
| ^^^^^^^^^^^^^^^^ help: try: `clear()`
error: aborting due to 4 previous errors