Skip to content

Commit

Permalink
Finish tests - add rustfix
Browse files Browse the repository at this point in the history
  • Loading branch information
bluthej committed Mar 22, 2023
1 parent 1d0acce commit e8ec242
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
2 changes: 0 additions & 2 deletions clippy_lints/src/methods/clear_with_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions tests/ui/clear_with_drain.fixed
Original file line number Diff line number Diff line change
@@ -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() {}
1 change: 1 addition & 0 deletions tests/ui/clear_with_drain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::clear_with_drain)]

Expand Down
28 changes: 28 additions & 0 deletions tests/ui/clear_with_drain.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e8ec242

Please sign in to comment.