Skip to content

Commit 25e2aff

Browse files
committed
UI test cleanup: Extract iter_nth tests
1 parent fdc2255 commit 25e2aff

File tree

4 files changed

+112
-99
lines changed

4 files changed

+112
-99
lines changed

tests/ui/iter_nth.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// aux-build:option_helpers.rs
2+
3+
#![warn(clippy::iter_nth)]
4+
5+
#[macro_use]
6+
extern crate option_helpers;
7+
8+
use option_helpers::IteratorFalsePositives;
9+
use std::collections::VecDeque;
10+
11+
/// Struct to generate false positives for things with `.iter()`.
12+
#[derive(Copy, Clone)]
13+
struct HasIter;
14+
15+
impl HasIter {
16+
fn iter(self) -> IteratorFalsePositives {
17+
IteratorFalsePositives { foo: 0 }
18+
}
19+
20+
fn iter_mut(self) -> IteratorFalsePositives {
21+
IteratorFalsePositives { foo: 0 }
22+
}
23+
}
24+
25+
/// Checks implementation of `ITER_NTH` lint.
26+
fn iter_nth() {
27+
let mut some_vec = vec![0, 1, 2, 3];
28+
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
29+
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
30+
31+
{
32+
// Make sure we lint `.iter()` for relevant types.
33+
let bad_vec = some_vec.iter().nth(3);
34+
let bad_slice = &some_vec[..].iter().nth(3);
35+
let bad_boxed_slice = boxed_slice.iter().nth(3);
36+
let bad_vec_deque = some_vec_deque.iter().nth(3);
37+
}
38+
39+
{
40+
// Make sure we lint `.iter_mut()` for relevant types.
41+
let bad_vec = some_vec.iter_mut().nth(3);
42+
}
43+
{
44+
let bad_slice = &some_vec[..].iter_mut().nth(3);
45+
}
46+
{
47+
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
48+
}
49+
50+
// Make sure we don't lint for non-relevant types.
51+
let false_positive = HasIter;
52+
let ok = false_positive.iter().nth(3);
53+
let ok_mut = false_positive.iter_mut().nth(3);
54+
}
55+
56+
fn main() {}

tests/ui/iter_nth.stderr

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
2+
--> $DIR/iter_nth.rs:33:23
3+
|
4+
LL | let bad_vec = some_vec.iter().nth(3);
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::iter-nth` implied by `-D warnings`
8+
9+
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
10+
--> $DIR/iter_nth.rs:34:26
11+
|
12+
LL | let bad_slice = &some_vec[..].iter().nth(3);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
16+
--> $DIR/iter_nth.rs:35:31
17+
|
18+
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
22+
--> $DIR/iter_nth.rs:36:29
23+
|
24+
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
28+
--> $DIR/iter_nth.rs:41:23
29+
|
30+
LL | let bad_vec = some_vec.iter_mut().nth(3);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
34+
--> $DIR/iter_nth.rs:44:26
35+
|
36+
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
40+
--> $DIR/iter_nth.rs:47:29
41+
|
42+
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: aborting due to 7 previous errors
46+

tests/ui/methods.rs

-45
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,6 @@ fn option_methods() {
215215
);
216216
}
217217

218-
/// Struct to generate false positives for things with `.iter()`.
219-
#[derive(Copy, Clone)]
220-
struct HasIter;
221-
222-
impl HasIter {
223-
fn iter(self) -> IteratorFalsePositives {
224-
IteratorFalsePositives { foo: 0 }
225-
}
226-
227-
fn iter_mut(self) -> IteratorFalsePositives {
228-
IteratorFalsePositives { foo: 0 }
229-
}
230-
}
231-
232218
/// Checks implementation of `FILTER_NEXT` lint.
233219
#[rustfmt::skip]
234220
fn filter_next() {
@@ -287,37 +273,6 @@ fn search_is_some() {
287273
let _ = foo.rposition().is_some();
288274
}
289275

290-
/// Checks implementation of `ITER_NTH` lint.
291-
fn iter_nth() {
292-
let mut some_vec = vec![0, 1, 2, 3];
293-
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
294-
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
295-
296-
{
297-
// Make sure we lint `.iter()` for relevant types.
298-
let bad_vec = some_vec.iter().nth(3);
299-
let bad_slice = &some_vec[..].iter().nth(3);
300-
let bad_boxed_slice = boxed_slice.iter().nth(3);
301-
let bad_vec_deque = some_vec_deque.iter().nth(3);
302-
}
303-
304-
{
305-
// Make sure we lint `.iter_mut()` for relevant types.
306-
let bad_vec = some_vec.iter_mut().nth(3);
307-
}
308-
{
309-
let bad_slice = &some_vec[..].iter_mut().nth(3);
310-
}
311-
{
312-
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
313-
}
314-
315-
// Make sure we don't lint for non-relevant types.
316-
let false_positive = HasIter;
317-
let ok = false_positive.iter().nth(3);
318-
let ok_mut = false_positive.iter_mut().nth(3);
319-
}
320-
321276
#[allow(clippy::similar_names)]
322277
fn main() {
323278
let opt = Some(0);

tests/ui/methods.stderr

+10-54
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ LL | });
154154
|
155155

156156
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
157-
--> $DIR/methods.rs:238:13
157+
--> $DIR/methods.rs:224:13
158158
|
159159
LL | let _ = v.iter().filter(|&x| *x < 0).next();
160160
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -163,7 +163,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
163163
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
164164

165165
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
166-
--> $DIR/methods.rs:241:13
166+
--> $DIR/methods.rs:227:13
167167
|
168168
LL | let _ = v.iter().filter(|&x| {
169169
| _____________^
@@ -173,7 +173,7 @@ LL | | ).next();
173173
| |___________________________^
174174

175175
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
176-
--> $DIR/methods.rs:257:13
176+
--> $DIR/methods.rs:243:13
177177
|
178178
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
179179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -182,7 +182,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
182182
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
183183

184184
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
185-
--> $DIR/methods.rs:260:13
185+
--> $DIR/methods.rs:246:13
186186
|
187187
LL | let _ = v.iter().find(|&x| {
188188
| _____________^
@@ -192,15 +192,15 @@ LL | | ).is_some();
192192
| |______________________________^
193193

194194
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
195-
--> $DIR/methods.rs:266:13
195+
--> $DIR/methods.rs:252:13
196196
|
197197
LL | let _ = v.iter().position(|&x| x < 0).is_some();
198198
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
199199
|
200200
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
201201

202202
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
203-
--> $DIR/methods.rs:269:13
203+
--> $DIR/methods.rs:255:13
204204
|
205205
LL | let _ = v.iter().position(|&x| {
206206
| _____________^
@@ -210,15 +210,15 @@ LL | | ).is_some();
210210
| |______________________________^
211211

212212
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
213-
--> $DIR/methods.rs:275:13
213+
--> $DIR/methods.rs:261:13
214214
|
215215
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
216216
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
217217
|
218218
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
219219

220220
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
221-
--> $DIR/methods.rs:278:13
221+
--> $DIR/methods.rs:264:13
222222
|
223223
LL | let _ = v.iter().rposition(|&x| {
224224
| _____________^
@@ -227,57 +227,13 @@ LL | | }
227227
LL | | ).is_some();
228228
| |______________________________^
229229

230-
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
231-
--> $DIR/methods.rs:298:23
232-
|
233-
LL | let bad_vec = some_vec.iter().nth(3);
234-
| ^^^^^^^^^^^^^^^^^^^^^^
235-
|
236-
= note: `-D clippy::iter-nth` implied by `-D warnings`
237-
238-
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
239-
--> $DIR/methods.rs:299:26
240-
|
241-
LL | let bad_slice = &some_vec[..].iter().nth(3);
242-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
243-
244-
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
245-
--> $DIR/methods.rs:300:31
246-
|
247-
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
248-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
249-
250-
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
251-
--> $DIR/methods.rs:301:29
252-
|
253-
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
254-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255-
256-
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
257-
--> $DIR/methods.rs:306:23
258-
|
259-
LL | let bad_vec = some_vec.iter_mut().nth(3);
260-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
261-
262-
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
263-
--> $DIR/methods.rs:309:26
264-
|
265-
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
266-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267-
268-
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
269-
--> $DIR/methods.rs:312:29
270-
|
271-
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
272-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
273-
274230
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
275-
--> $DIR/methods.rs:324:13
231+
--> $DIR/methods.rs:279:13
276232
|
277233
LL | let _ = opt.unwrap();
278234
| ^^^^^^^^^^^^
279235
|
280236
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
281237

282-
error: aborting due to 32 previous errors
238+
error: aborting due to 25 previous errors
283239

0 commit comments

Comments
 (0)