Skip to content

Commit 39bd844

Browse files
committed
Auto merge of #3605 - phansch:small_methods_ui_test_improvement, r=flip1995
UI test cleanup: Extract iter_skip_next from methods.rs cc #2038
2 parents 61bfb39 + 8c4c458 commit 39bd844

File tree

4 files changed

+91
-40
lines changed

4 files changed

+91
-40
lines changed

tests/ui/iter_skip_next.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![warn(clippy::iter_skip_next)]
11+
#![allow(clippy::blacklisted_name)]
12+
13+
/// Struct to generate false positive for Iterator-based lints
14+
#[derive(Copy, Clone)]
15+
struct IteratorFalsePositives {
16+
foo: u32,
17+
}
18+
19+
impl IteratorFalsePositives {
20+
fn filter(self) -> IteratorFalsePositives {
21+
self
22+
}
23+
24+
fn next(self) -> IteratorFalsePositives {
25+
self
26+
}
27+
28+
fn find(self) -> Option<u32> {
29+
Some(self.foo)
30+
}
31+
32+
fn position(self) -> Option<u32> {
33+
Some(self.foo)
34+
}
35+
36+
fn rposition(self) -> Option<u32> {
37+
Some(self.foo)
38+
}
39+
40+
fn nth(self, n: usize) -> Option<u32> {
41+
Some(self.foo)
42+
}
43+
44+
fn skip(self, _: usize) -> IteratorFalsePositives {
45+
self
46+
}
47+
}
48+
49+
/// Checks implementation of `ITER_SKIP_NEXT` lint
50+
fn iter_skip_next() {
51+
let mut some_vec = vec![0, 1, 2, 3];
52+
let _ = some_vec.iter().skip(42).next();
53+
let _ = some_vec.iter().cycle().skip(42).next();
54+
let _ = (1..10).skip(10).next();
55+
let _ = &some_vec[..].iter().skip(3).next();
56+
let foo = IteratorFalsePositives { foo: 0 };
57+
let _ = foo.skip(42).next();
58+
let _ = foo.filter().skip(42).next();
59+
}
60+
61+
fn main() {}

tests/ui/iter_skip_next.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
2+
--> $DIR/iter_skip_next.rs:52:13
3+
|
4+
LL | let _ = some_vec.iter().skip(42).next();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
8+
9+
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
10+
--> $DIR/iter_skip_next.rs:53:13
11+
|
12+
LL | let _ = some_vec.iter().cycle().skip(42).next();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
16+
--> $DIR/iter_skip_next.rs:54:13
17+
|
18+
LL | let _ = (1..10).skip(10).next();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
22+
--> $DIR/iter_skip_next.rs:55:14
23+
|
24+
LL | let _ = &some_vec[..].iter().skip(3).next();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
28+

tests/ui/methods.rs

-12
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,6 @@ fn iter_nth() {
390390
let ok_mut = false_positive.iter_mut().nth(3);
391391
}
392392

393-
/// Checks implementation of `ITER_SKIP_NEXT` lint
394-
fn iter_skip_next() {
395-
let mut some_vec = vec![0, 1, 2, 3];
396-
let _ = some_vec.iter().skip(42).next();
397-
let _ = some_vec.iter().cycle().skip(42).next();
398-
let _ = (1..10).skip(10).next();
399-
let _ = &some_vec[..].iter().skip(3).next();
400-
let foo = IteratorFalsePositives { foo : 0 };
401-
let _ = foo.skip(42).next();
402-
let _ = foo.filter().skip(42).next();
403-
}
404-
405393
#[allow(clippy::similar_names)]
406394
fn main() {
407395
let opt = Some(0);

tests/ui/methods.stderr

+2-28
Original file line numberDiff line numberDiff line change
@@ -367,39 +367,13 @@ error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both fa
367367
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
368368
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
369369

370-
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
371-
--> $DIR/methods.rs:396:13
372-
|
373-
LL | let _ = some_vec.iter().skip(42).next();
374-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375-
|
376-
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
377-
378-
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
379-
--> $DIR/methods.rs:397:13
380-
|
381-
LL | let _ = some_vec.iter().cycle().skip(42).next();
382-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
383-
384-
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
385-
--> $DIR/methods.rs:398:13
386-
|
387-
LL | let _ = (1..10).skip(10).next();
388-
| ^^^^^^^^^^^^^^^^^^^^^^^
389-
390-
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
391-
--> $DIR/methods.rs:399:14
392-
|
393-
LL | let _ = &some_vec[..].iter().skip(3).next();
394-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395-
396370
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
397-
--> $DIR/methods.rs:408:13
371+
--> $DIR/methods.rs:396:13
398372
|
399373
LL | let _ = opt.unwrap();
400374
| ^^^^^^^^^^^^
401375
|
402376
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
403377

404-
error: aborting due to 50 previous errors
378+
error: aborting due to 46 previous errors
405379

0 commit comments

Comments
 (0)