Skip to content

Commit

Permalink
Move fixable filter_next and filter_map_next cases to rustfixed t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
ThibsG committed Oct 23, 2020
1 parent e75534e commit 9a203c9
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 35 deletions.
3 changes: 0 additions & 3 deletions tests/ui/filter_map_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
fn main() {
let a = ["1", "lol", "3", "NaN", "5"];

let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
assert_eq!(element, Some(1));

#[rustfmt::skip]
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
.into_iter()
Expand Down
14 changes: 4 additions & 10 deletions tests/ui/filter_map_next.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead.
--> $DIR/filter_map_next.rs:6:32
|
LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `a.iter().find_map(|s| s.parse().ok())`
|
= note: `-D clippy::filter-map-next` implied by `-D warnings`

error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead.
--> $DIR/filter_map_next.rs:10:26
--> $DIR/filter_map_next.rs:7:26
|
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
| __________________________^
Expand All @@ -18,6 +10,8 @@ LL | | if x == 2 {
LL | | })
LL | | .next();
| |_______________^
|
= note: `-D clippy::filter-map-next` implied by `-D warnings`

error: aborting due to 2 previous errors
error: aborting due to previous error

10 changes: 10 additions & 0 deletions tests/ui/filter_map_next_fixable.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix

#![warn(clippy::all, clippy::pedantic)]

fn main() {
let a = ["1", "lol", "3", "NaN", "5"];

let element: Option<i32> = a.iter().find_map(|s| s.parse().ok());
assert_eq!(element, Some(1));
}
10 changes: 10 additions & 0 deletions tests/ui/filter_map_next_fixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix

#![warn(clippy::all, clippy::pedantic)]

fn main() {
let a = ["1", "lol", "3", "NaN", "5"];

let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
assert_eq!(element, Some(1));
}
10 changes: 10 additions & 0 deletions tests/ui/filter_map_next_fixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead.
--> $DIR/filter_map_next_fixable.rs:8:32
|
LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `a.iter().find_map(|s| s.parse().ok())`
|
= note: `-D clippy::filter-map-next` implied by `-D warnings`

error: aborting due to previous error

5 changes: 1 addition & 4 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,13 @@ impl Mul<T> for T {
fn filter_next() {
let v = vec![3, 2, 1, 0, -1, -2, -3];

// Single-line case.
let _ = v.iter().filter(|&x| *x < 0).next();

// Multi-line case.
let _ = v.iter().filter(|&x| {
*x < 0
}
).next();

// Check that hat we don't lint if the caller is not an `Iterator`.
// Check that we don't lint if the caller is not an `Iterator`.
let foo = IteratorFalsePositives { foo: 0 };
let _ = foo.filter().next();
}
Expand Down
30 changes: 12 additions & 18 deletions tests/ui/methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,43 @@ LL | | }
error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead.
--> $DIR/methods.rs:126:13
|
LL | let _ = v.iter().filter(|&x| *x < 0).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `v.iter().find(|&x| *x < 0)`
|
= note: `-D clippy::filter-next` implied by `-D warnings`

error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead.
--> $DIR/methods.rs:129:13
|
LL | let _ = v.iter().filter(|&x| {
| _____________^
LL | | *x < 0
LL | | }
LL | | ).next();
| |___________________________^
|
= note: `-D clippy::filter-next` implied by `-D warnings`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:146:22
--> $DIR/methods.rs:143:22
|
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| *x < 0)`
|
= note: `-D clippy::search-is-some` implied by `-D warnings`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:147:20
--> $DIR/methods.rs:144:20
|
LL | let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| **y == x)`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:148:20
--> $DIR/methods.rs:145:20
|
LL | let _ = (0..1).find(|x| *x == 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `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:149:22
--> $DIR/methods.rs:146:22
|
LL | let _ = v.iter().find(|x| **x == 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `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:152:13
--> $DIR/methods.rs:149:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
Expand All @@ -63,13 +57,13 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:158:22
--> $DIR/methods.rs:155:22
|
LL | let _ = v.iter().position(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `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:161:13
--> $DIR/methods.rs:158:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
Expand All @@ -79,13 +73,13 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:167:22
--> $DIR/methods.rs:164:22
|
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `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:170:13
--> $DIR/methods.rs:167:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
Expand All @@ -94,5 +88,5 @@ LL | | }
LL | | ).is_some();
| |______________________________^

error: aborting due to 12 previous errors
error: aborting due to 11 previous errors

11 changes: 11 additions & 0 deletions tests/ui/methods_fixable.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix

#![warn(clippy::filter_next)]

/// Checks implementation of `FILTER_NEXT` lint.
fn main() {
let v = vec![3, 2, 1, 0, -1, -2, -3];

// Single-line case.
let _ = v.iter().find(|&x| *x < 0);
}
11 changes: 11 additions & 0 deletions tests/ui/methods_fixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix

#![warn(clippy::filter_next)]

/// Checks implementation of `FILTER_NEXT` lint.
fn main() {
let v = vec![3, 2, 1, 0, -1, -2, -3];

// Single-line case.
let _ = v.iter().filter(|&x| *x < 0).next();
}
10 changes: 10 additions & 0 deletions tests/ui/methods_fixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead.
--> $DIR/methods_fixable.rs:10:13
|
LL | let _ = v.iter().filter(|&x| *x < 0).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `v.iter().find(|&x| *x < 0)`
|
= note: `-D clippy::filter-next` implied by `-D warnings`

error: aborting due to previous error

0 comments on commit 9a203c9

Please sign in to comment.