mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 09:23:05 +00:00
UI test cleanup: Extract iter_nth tests
This commit is contained in:
parent
fdc2255e81
commit
25e2affd31
56
tests/ui/iter_nth.rs
Normal file
56
tests/ui/iter_nth.rs
Normal file
@ -0,0 +1,56 @@
|
||||
// aux-build:option_helpers.rs
|
||||
|
||||
#![warn(clippy::iter_nth)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate option_helpers;
|
||||
|
||||
use option_helpers::IteratorFalsePositives;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
/// Struct to generate false positives for things with `.iter()`.
|
||||
#[derive(Copy, Clone)]
|
||||
struct HasIter;
|
||||
|
||||
impl HasIter {
|
||||
fn iter(self) -> IteratorFalsePositives {
|
||||
IteratorFalsePositives { foo: 0 }
|
||||
}
|
||||
|
||||
fn iter_mut(self) -> IteratorFalsePositives {
|
||||
IteratorFalsePositives { foo: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks implementation of `ITER_NTH` lint.
|
||||
fn iter_nth() {
|
||||
let mut some_vec = vec![0, 1, 2, 3];
|
||||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
|
||||
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
|
||||
|
||||
{
|
||||
// Make sure we lint `.iter()` for relevant types.
|
||||
let bad_vec = some_vec.iter().nth(3);
|
||||
let bad_slice = &some_vec[..].iter().nth(3);
|
||||
let bad_boxed_slice = boxed_slice.iter().nth(3);
|
||||
let bad_vec_deque = some_vec_deque.iter().nth(3);
|
||||
}
|
||||
|
||||
{
|
||||
// Make sure we lint `.iter_mut()` for relevant types.
|
||||
let bad_vec = some_vec.iter_mut().nth(3);
|
||||
}
|
||||
{
|
||||
let bad_slice = &some_vec[..].iter_mut().nth(3);
|
||||
}
|
||||
{
|
||||
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
|
||||
}
|
||||
|
||||
// Make sure we don't lint for non-relevant types.
|
||||
let false_positive = HasIter;
|
||||
let ok = false_positive.iter().nth(3);
|
||||
let ok_mut = false_positive.iter_mut().nth(3);
|
||||
}
|
||||
|
||||
fn main() {}
|
46
tests/ui/iter_nth.stderr
Normal file
46
tests/ui/iter_nth.stderr
Normal file
@ -0,0 +1,46 @@
|
||||
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:33:23
|
||||
|
|
||||
LL | let bad_vec = some_vec.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::iter-nth` implied by `-D warnings`
|
||||
|
||||
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:34:26
|
||||
|
|
||||
LL | let bad_slice = &some_vec[..].iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:35:31
|
||||
|
|
||||
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:36:29
|
||||
|
|
||||
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:41:23
|
||||
|
|
||||
LL | let bad_vec = some_vec.iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:44:26
|
||||
|
|
||||
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/iter_nth.rs:47:29
|
||||
|
|
||||
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
@ -215,20 +215,6 @@ fn option_methods() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Struct to generate false positives for things with `.iter()`.
|
||||
#[derive(Copy, Clone)]
|
||||
struct HasIter;
|
||||
|
||||
impl HasIter {
|
||||
fn iter(self) -> IteratorFalsePositives {
|
||||
IteratorFalsePositives { foo: 0 }
|
||||
}
|
||||
|
||||
fn iter_mut(self) -> IteratorFalsePositives {
|
||||
IteratorFalsePositives { foo: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks implementation of `FILTER_NEXT` lint.
|
||||
#[rustfmt::skip]
|
||||
fn filter_next() {
|
||||
@ -287,37 +273,6 @@ fn search_is_some() {
|
||||
let _ = foo.rposition().is_some();
|
||||
}
|
||||
|
||||
/// Checks implementation of `ITER_NTH` lint.
|
||||
fn iter_nth() {
|
||||
let mut some_vec = vec![0, 1, 2, 3];
|
||||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
|
||||
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
|
||||
|
||||
{
|
||||
// Make sure we lint `.iter()` for relevant types.
|
||||
let bad_vec = some_vec.iter().nth(3);
|
||||
let bad_slice = &some_vec[..].iter().nth(3);
|
||||
let bad_boxed_slice = boxed_slice.iter().nth(3);
|
||||
let bad_vec_deque = some_vec_deque.iter().nth(3);
|
||||
}
|
||||
|
||||
{
|
||||
// Make sure we lint `.iter_mut()` for relevant types.
|
||||
let bad_vec = some_vec.iter_mut().nth(3);
|
||||
}
|
||||
{
|
||||
let bad_slice = &some_vec[..].iter_mut().nth(3);
|
||||
}
|
||||
{
|
||||
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
|
||||
}
|
||||
|
||||
// Make sure we don't lint for non-relevant types.
|
||||
let false_positive = HasIter;
|
||||
let ok = false_positive.iter().nth(3);
|
||||
let ok_mut = false_positive.iter_mut().nth(3);
|
||||
}
|
||||
|
||||
#[allow(clippy::similar_names)]
|
||||
fn main() {
|
||||
let opt = Some(0);
|
||||
|
@ -154,7 +154,7 @@ LL | });
|
||||
|
|
||||
|
||||
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
|
||||
--> $DIR/methods.rs:238:13
|
||||
--> $DIR/methods.rs:224:13
|
||||
|
|
||||
LL | let _ = v.iter().filter(|&x| *x < 0).next();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -163,7 +163,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
|
||||
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
|
||||
|
||||
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
|
||||
--> $DIR/methods.rs:241:13
|
||||
--> $DIR/methods.rs:227:13
|
||||
|
|
||||
LL | let _ = v.iter().filter(|&x| {
|
||||
| _____________^
|
||||
@ -173,7 +173,7 @@ LL | | ).next();
|
||||
| |___________________________^
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:257:13
|
||||
--> $DIR/methods.rs:243:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -182,7 +182,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:260:13
|
||||
--> $DIR/methods.rs:246:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| {
|
||||
| _____________^
|
||||
@ -192,7 +192,7 @@ LL | | ).is_some();
|
||||
| |______________________________^
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:266:13
|
||||
--> $DIR/methods.rs:252:13
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -200,7 +200,7 @@ LL | let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:269:13
|
||||
--> $DIR/methods.rs:255:13
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| {
|
||||
| _____________^
|
||||
@ -210,7 +210,7 @@ LL | | ).is_some();
|
||||
| |______________________________^
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:275:13
|
||||
--> $DIR/methods.rs:261:13
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -218,7 +218,7 @@ LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
|
||||
--> $DIR/methods.rs:278:13
|
||||
--> $DIR/methods.rs:264:13
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| {
|
||||
| _____________^
|
||||
@ -227,57 +227,13 @@ LL | | }
|
||||
LL | | ).is_some();
|
||||
| |______________________________^
|
||||
|
||||
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/methods.rs:298:23
|
||||
|
|
||||
LL | let bad_vec = some_vec.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::iter-nth` implied by `-D warnings`
|
||||
|
||||
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/methods.rs:299:26
|
||||
|
|
||||
LL | let bad_slice = &some_vec[..].iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/methods.rs:300:31
|
||||
|
|
||||
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
|
||||
--> $DIR/methods.rs:301:29
|
||||
|
|
||||
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/methods.rs:306:23
|
||||
|
|
||||
LL | let bad_vec = some_vec.iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/methods.rs:309:26
|
||||
|
|
||||
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
|
||||
--> $DIR/methods.rs:312:29
|
||||
|
|
||||
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
|
||||
--> $DIR/methods.rs:324:13
|
||||
--> $DIR/methods.rs:279:13
|
||||
|
|
||||
LL | let _ = opt.unwrap();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
|
||||
|
||||
error: aborting due to 32 previous errors
|
||||
error: aborting due to 25 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user